GetDotted Domains

Viewing Thread:
".htaccess 301 redirect problem"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Thu 17/11/11 at 15:50
Regular
Posts: 1
Hi

I'm trying to redirect some old URLs to new ones in my .htaccess file using 301 redirects.

If the old URL ends in a file extension or a trailing slash:


RewriteBase /

RewriteRule ^old_page.html$ new_page.php [R=301,L]
RewriteRule ^old_page/$ new_page.php [R=301,L]


The redirects work as expected, i.e.:

www.mysite.com/old_page.html redirects in browser to www.mysite.com/new_page.php
www.mysite.com/old_page/ also redirects in browser to www.mysite.com/new_page.php

However, if the old URL does not end in a file extension or forward slash, the redirects do not work:


RewriteRule ^old_page/$ new_page.php [R=301,L]


www.mysite.com/old_page in browser returns 404 not found

On examining the rewrite error logs, it seems that the server is throwing a "filedoesnotexist error" for "old_page" before it even attempts to perform the redirect.

I've done similar things with other web hosts, and never encountered this problem before. I'm on shared VIP hosting - could it be due to a setting in the server's httpd.conf file, possibly with Allowoverride All|None|... or similar?

Cheers
Joel
Thu 17/11/11 at 16:28
Staff Moderator
"Mr Dean"
Posts: 170
I'm not an expert on rewrite rules and regular expressions, but I'd expect the following to cause all three scenarios to work.

RewriteEngine on
RewriteBase /

RewriteRule ^old_page.html$ new_page.php [R=301,L]
RewriteRule ^old_page/?$ new_page.php [R=301,L]


The '?' just makes the trailing slash optional.
Thu 17/11/11 at 16:05
Regular
"Devil in disguise"
Posts: 3,151
DogAndAHalf wrote:
However, if the old URL does not end in a file extension or forward slash, the redirects do not work:


RewriteRule ^old_page/$ new_page.php [R=301,L]


www.mysite.com/old_page in browser returns 404 not found


Some clarification needed I think. That rule isnt going to match that URL so a 404 is expected if the old page doesnt exist anymore?

Do you really mean that rewrite rule should match to www.mysite.com/old_page or have you just copy/pasted it wrongly?
Thu 17/11/11 at 15:58
Moderator
"Are you sure?"
Posts: 5,000
Hi and welcome to the forums!

There are probably different ways to fix this - but here's my simple method!

Surely the old location has actually got a file type?

i.e. My URL www.hmmm.ip3.co.uk/camera/ can also be found by typing
www.hmmm.ip3.co.uk/camera/index.php - obviously you may have to change the file type to htm/html/shtml or whatever you are using.


[s]Hmmm...[/s]
Thu 17/11/11 at 15:50
Regular
Posts: 1
Hi

I'm trying to redirect some old URLs to new ones in my .htaccess file using 301 redirects.

If the old URL ends in a file extension or a trailing slash:


RewriteBase /

RewriteRule ^old_page.html$ new_page.php [R=301,L]
RewriteRule ^old_page/$ new_page.php [R=301,L]


The redirects work as expected, i.e.:

www.mysite.com/old_page.html redirects in browser to www.mysite.com/new_page.php
www.mysite.com/old_page/ also redirects in browser to www.mysite.com/new_page.php

However, if the old URL does not end in a file extension or forward slash, the redirects do not work:


RewriteRule ^old_page/$ new_page.php [R=301,L]


www.mysite.com/old_page in browser returns 404 not found

On examining the rewrite error logs, it seems that the server is throwing a "filedoesnotexist error" for "old_page" before it even attempts to perform the redirect.

I've done similar things with other web hosts, and never encountered this problem before. I'm on shared VIP hosting - could it be due to a setting in the server's httpd.conf file, possibly with Allowoverride All|None|... or similar?

Cheers
Joel

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Thank you very much for your help!
Top service for free - excellent - thank you very much for your help.
First Class!
I feel that your service on this occasion was absolutely first class - a model of excellence. After this, I hope to stay with Freeola for a long time!

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.