Forum Moderators: phranque

Message Too Old, No Replies

Don't know how to 301 redirect; 301 gives errors

Can't get 301 to work using .htaccess

         

DennyTang

10:15 pm on Mar 26, 2005 (gmt 0)

10+ Year Member



I'm trying to use a 301 redirect rule to update all the urls but it alway gives me this message:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator to inform of the time the error occurred and of anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

I'm just trying simple codes just to try and get it working but I really need a lot of redirecting rules to be set.

Here's my .htaccess file just incase I'm doing something wrong:

RewriteEngine on
RedirectPermanent folder/index.htm [mydomain.com...]

My domain isn't going to be changing, just the folders and files inside it. Here's an example of what I want to do after I can get the 301 working:

Change:
•mydomain.com/tutorials/photoshop to mydomain.com/v2/adobe-photoshop
•mydomain.com/tutorials/photoshop/photographic_effects to mydomain.com/v2/adobe-photoshop/photographic-effects/
•mydomain.com/tutorials/photoshop/photographic_effects/tutorial_name/ to mydomain.com/v2/adobe-photoshop/photographic-effects/tutorial-name.html
•mydomain.com/tutorials/photoshop/photographic_effects/tutorial_name/index.htm to mydomain.com/v2/adobe-photoshop/photographic-effects/tutorial-name.html

and so on for every folder.

Span

11:44 pm on Mar 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Denny,
since you're not actually rewriting but only redirecting, there's no need to wake up mod rewrite... take out this line: RewriteEngine on

DennyTang

12:09 am on Mar 27, 2005 (gmt 0)

10+ Year Member



Thanks
But I can't even get a simple redirect to work. It keeps giving me the error.

Redirect 301 directory/file.htm directory/v2/file.htm

Span

12:14 am on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, you should write a simple redirect like this:

Redirect 301 /directory/file.htm [domain.tld...]

At the left a relative URL, at the right an absolute one.

DennyTang

12:54 am on Mar 27, 2005 (gmt 0)

10+ Year Member



Thanks that works. Instead of an absolute URL for the redirection URL, can I use a relative URL?

jdMorgan

1:59 am on Mar 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Can I use a relative URL?

No, the Redirect family of directives requires a full URL, because these directives produce an external redirect response.

What change caused your redirect to work?

It may be that you don't have permission to use mod_rewrite. If you put RewriteEngine on back in and your server starts getting the error again, then add an options statement, and try this:


Options +FollowSymLinks
RewriteEngine on
RewriteRule ^folder/index\.htm$ http://mydomain.com/v2/folder/filename.htm [R=301,L]

If that fails, then please post your Apache version number and the relevant contents of your server error log file.

If it turns out that you're not allowed to use mod_rewrite, then you may be able to accomplish most of your goals using RedirectMatch [httpd.apache.org].

Jim

DennyTang

9:47 pm on Mar 27, 2005 (gmt 0)

10+ Year Member



Thanks jdMorgan for your reply

Sorry it's not working again. I dont really know what's going on, sometimes it gives me the error and sometimes it doesn't.

I just wrote hundreds of lines of redirect 301

I haven't tried the code you've suggested yet jdMorgan because I really hope that the one I already wrote works. It took along time to write it.

[edited by: jdMorgan at 4:57 am (utc) on Mar. 28, 2005]
[edit reason] Removed site URL. Please see TOS. [/edit]

jdMorgan

4:58 am on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post your Apache version number and the relevant contents of your server error log file.

DennyTang

7:34 am on Mar 28, 2005 (gmt 0)

10+ Year Member



I only have access to plesk. Do I have to ask my host for this information?

DennyTang

2:27 am on Mar 29, 2005 (gmt 0)

10+ Year Member



K it turns out it was errors on the code. With about 400 lines of stuff.. there has to be a typo somewhere and I fixed quite a few of them. Everything works now but how come when I try this code it gives me a looping effect?

redirect 301 / [mydomain.com...]

It goes to the same domain.. I just want to tell it to go to the v2 folder in the domain.

DennyTang

5:05 am on Mar 29, 2005 (gmt 0)

10+ Year Member



Is there anyway to get around this?

redirect 301 /folder1/folder2 [domain.com...]
redirect 301 /folder1/folder2/filename.htm [domain.com...]

The 2nd line doesnt run because it executes hte first line first.

jdMorgan

5:42 am on Mar 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it gives me a looping effect?

redirect 301 / http://mydomain.com/v2

Yes, it will. The Redirect directive uses prefix-matching. So any requested URL that starts with a slash will be redirected to /v2, including /v2 itself.

The problem is that there's no 'pretty' way to fix this without mod_rewrite or server config access. An ugly way to fix it might be:


RedirectMatch ^/([^v][^2]?.*)$ http://mydomain.com/v2/$1

Is there anyway to get around this?

redirect 301 /folder1/folder2 http://domain.com/v2/folder1/folder2
redirect 301 /folder1/folder2/filename.htm http://domain.com/v2/folder1/folder2/filename.htm

The 2nd line doesnt run because it executes hte first line first.

Again, this is because of prefix-matching.

Reverse the order of the two directives -- Always put your most-specific directives first. Or use RedirectMatch instead of Redirect.

Jim

DennyTang

6:41 pm on Mar 30, 2005 (gmt 0)

10+ Year Member



Thanks JDMorgan for your help. That works and my website is almost all fine now! But I got over 10,000 hits to my 404 page yesterday and after figuring out why, I found out that it doesnt' really work that well.

Heres the script that doesnt work. The 2nd line will run but the 3rd line will end up something like this [domain.com...]

redirect 301 /tutorials/photoshop/photo_retouching/auto_white_balance/index.htm [fotofects.com...]
redirect 301 /tutorials/photoshop/photo_retouching [fotofects.com...]

It's not too important to get that link working so if it's too much work to figure out how to do it, then it's fine.

jdMorgan

9:30 pm on Mar 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, I'd suggest you look into using RedirectMatch [httpd.apache.org]. In addition to solving your current problem, RedirectMatch will also let you reduce the number of directives in your code to about 20% of what you have now.

For example, this one line:


RedirectMatch 301 ^/tutorials/photoshop/fractals/([^_]+)_([^/]+)/index.htm$ http://example.com/v2/adobe-photoshop/fractals/$1-$2.html

can replace all of these:

#Redirect 301 /tutorials/photoshop/fractals/florid_paint/index.htm http://example.com/v2/adobe-photoshop/fractals/florid-paint.html
#Redirect 301 /tutorials/photoshop/fractals/lace_flower/index.htm http://example.com/v2/adobe-photoshop/fractals/lace-flower.html
#Redirect 301 /tutorials/photoshop/fractals/liquified_kaleido/index.htm http://example.com/v2/adobe-photoshop/fractals/liquified-kaleido.html
#Redirect 301 /tutorials/photoshop/fractals/ornate_rug/index.htm http://example.com/v2/adobe-photoshop/fractals/ornate-rug.html
#Redirect 301 /tutorials/photoshop/fractals/powdered_butterflies/index.htm http://example.com/v2/adobe-photoshop/fractals/powdered-butterflies.html
#Redirect 301 /tutorials/photoshop/fractals/rectangular_slinky/index.htm http://example.com/v2/adobe-photoshop/fractals/rectangular-slinky.html
#Redirect 301 /tutorials/photoshop/fractals/scaling_boxes/index.htm http://example.com/v2/adobe-photoshop/fractals/scaling-boxes.html
#Redirect 301 /tutorials/photoshop/fractals/spoked_tunnel/index.htm http://example.com/v2/adobe-photoshop/fractals/spoked-tunnel.html
#Redirect 301 /tutorials/photoshop/fractals/translucent_feather/index.htm http://example.com/v2/adobe-photoshop/fractals/translucent-feather.html
#Redirect 301 /tutorials/photoshop/fractals/webbed_flower/index.htm http://example.com/v2/adobe-photoshop/fractals/webbed-flower.html

Because we can't look over your shoulder while you write and test the code, you'll have to study the documentation and learn to do this for yourself. Only you can decide if it is worth your time to do so, but I thought I'd mention it so that you have the option (the shorter code will run much faster).

The trick is to take your current Rewrite directives and sort them first alphabetically, and then by the number of "variables" in the URL. Putting them in some kind of order will allow you to find "shortcuts" like the one I used above.

Jim

DennyTang

2:41 am on Apr 2, 2005 (gmt 0)

10+ Year Member



Thank you very much for your informative help jdmorgan.