.htaccess & redirects


#1

Hi

I having a few problems with .htaccess and redirects. I have a area where i keep all my banners and images I use on forums:

mysite.net/forums/avatars
mysite.net/forums/sigs

Now I need the files in there to be linked to, but I want to deny a directory listing. using
Options -indexes in .htaccess works fine but then I thought instead of just getting a 403 error, why not redirect the request to www.mysite.net

This is where my problem is.
I setup a forbidden.html and that worked.
I then used .htaccess to

Redirect forbidden.html http://mysite.net

but the redirect did not work. I just kept getting the 403 error. I also tried putting a .htaccess in mysite.net/forums/avatars but again the redirect did not work.

Any advice?


#2

[quote]Now I need the files in there to be linked to, but I want to deny a directory listing … Any advice?

[/quote]

You know what? If you only have a couple of directories to take care of, a really simple solution would be to create an index.html, holding whatever you like.

To make it LOOK professional, you may even put in some text like what I did with mine here:

http://www.nameslave.com/includes/


[color=#CC0000]name[/color]slave
Domain Registration, Web Hosting & Search Engine Optimization


#3

[quote]I then used .htaccess to

Redirect forbidden.html http://mysite.net

but the redirect did not work. I just kept getting the 403 error. I also tried putting a .htaccess in mysite.net/forums/avatars but again the redirect did not work.
[/quote]
Just put the following in an .htaccess file and put that in your graphics directories.

Options -Indexes ErrorDocument 403 http://mysite.net/The web server will neither send status code 200 or 403 - it will send an actual redirect using 302 “Found” instead.

See Apache HTTP Server Version 1.3 - ErrorDocument directive

:cool: Perl / MySQL / HTML CSS


#4

Thanks Atropos7

Worked like a dream :slight_smile:


#5

something even better would be one that looks the same as the rest of the site like what i did

http://www.rawkstar.net/layout/ - 403
http://www.rawkstar.net/fnar - 404

www.rawkstar.net


#6

How do you make the ‘illegally’ requested filename/dir appear on your error messages, Bob? I have come across similar customised error pages which even display the full URL of the requested file/dir.
Cheers,
Marsbar


#7

You want to use the environment variable REQUEST_URI set by the web server.

To do this using server-parsed HTML documents:

[code]

HTTP 404 - Not Found

Not Found

Just kidding - this is what you were looking for!

could not be found. Take your pick:

  • It was probably never here in the first place.
  • You're too late and now it's gone.
[/code]Save that as missing.shtml then use [code]ErrorDocument 404 missing.shtml[/code]in .htaccess

Example:
http://openvein.org/foobar - 404 Not found
http://openvein.org/error/missing.shtml - 200 OK

In Perl, you would use $ENV{‘REQUEST_URI’} and I am not sure about PHP.

:cool: Perl / MySQL / HTML CSS


#8

You site looks great Madbob but I just wanted 403 errors sent to mysite.net/index.html

Tancred


#9

yeah i wasn’t really replying to the original topic, i have a habbit of doing that! :smiley:

But on topic, that would annoy me… the 403/404 redirecting me to the index.html file. i.e.

if i were to type in http://www.mysite.com/nwes (intended to be news) and it redirected me to the index page i would get confused as to what i did wrong (if i never noticed that i typed news wrong…) this would be more annoying if i were looking for an image in say, layout/images/backround/img.jpg and had to type all that again if i misssed a j in jpg.

I know i’m rambling now so i’ll shut up! But i do think it’s better to show the user they’ve clicked a broken link typed the url incorrectly.

www.rawkstar.net


#10

in php: $_SERVER[‘REQUEST_URI’] - close to perl, i.e. what Atropos7 said!

I plan to expand it to show files with similar names that the person could be looking for… but the site doesn’t have enough on it at the moment to justify me writing that!

www.rawkstar.net


#11

I don’t think it’s really a very good idea to configure your site to suppress the normal HTTP response codes for things like “404 Not Found” and “Forbidden”, etc., since those are used by such things as search engine indexing robots and automated link checkers. Without the proper response codes returned, the invalid URLs in your site may get into search engines, be linked to, etc., and none of the automated processes will ever notice they’re invalid and delink them.

– Dan


#12

REQUEST_URI, you beauty!! Thanks awfully for your help, Atropos7.
-Marsbar


#13

I’d thought I would take a moment to clarify what you mean above.

Using a default index page or using a redirect will never indicate to a robot that the resource is “404 Not Found” or “403 Forbidden” or any other error code because those two methods issue their own status codes: “200 OK” and “302 Found” respectively.

If you use ErrorDocument directive with a local URI or string it will preserve the status code and allow for you to customize the page content. This plays nice with robots and allows you to inform humans in a friendly manner of what happened.

Not to forget that Internet Explorer will show its own error pages unless yours is at least 512 bytes.

:cool: Perl / MySQL / HTML+CSS


#14

Madbob wrote

and dtobias wrote

You both have good points. In my case I have a directory full of files I use on forums. I have setup a subdomain just for this and have a robots.txt file set to deny all, so it should never be in a good search engine.

I just want to stop people viewing my images on the forum, then looking throught the directory and linking to any file they like :slight_smile: