Forum Moderators: phranque

Message Too Old, No Replies

Problems with .htaccess

500 error when trying to implement a 301

         

paddy_89

12:10 pm on Nov 9, 2010 (gmt 0)

10+ Year Member



okay so a short while ago I was asked to put a 301 redirect from an old site to a new site, which I did, and it worked. However the problem was the redirect made all pages go to the index page of the new site.

I've tried doing a page by page solution but it keeps giving me 500 errors when I try it


The Working .htaccess file is as follows:

Options +FollowSymlinks
RewriteEngine on

Rewritecond %{HTTP_HOST} ^oldsite.com [NC]
Rewriterule ^(.*)$ http://www.newsite.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]


I've tried using this:

http://www.htaccesseditor.com/en.shtml#a_redirect


which didn't work

I've tried this code:

redirect 301 /index.html http://www.newsite.com/


which again gace a 500 error

and I've tried using the rewrite code as above like so:


The Working .htaccess file is as follows:

Options +FollowSymlinks
RewriteEngine on

Rewritecond %{HTTP_HOST} ^oldsite.com/page.html [NC]
Rewriterule ^(.*)$ http://www.newsite.com/page.html$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.oldsite.com/page.html [NC]
RewriteRule ^(.*)$ http://www.newsite.com/page.html$1 [L,R=301]

Rewritecond %{HTTP_HOST} ^oldsite.com [NC]
Rewriterule ^(.*)$ http://www.newsite.com/$1 [R=301,NC]
RewriteCond %{HTTP_HOST} ^www.oldsite.com [NC]
RewriteRule ^(.*)$ http://www.newsite.com/$1 [L,R=301]



after causing numerous 500 errors I've decided that I need help! Any input appreciated.

sublime1

2:44 am on Nov 13, 2010 (gmt 0)

10+ Year Member



So let's define the problem a little more clearly. If the case is that you have an identical site on a new domain, so that, for example


[olddomain.com...] => [newdomain.com...]


This is a simple case, semantically identical to domain canonicalization,


RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.com/$1 [R=301,L]


But if there are different URLs on the new domain, then you'll need to do more.

Chances are there's something else -- what kind of environment are you working in (e.g. WordPress, Drupal, etc.)?

Tom

jdMorgan

12:53 am on Nov 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The very first thing to do when you get a 500-Server Error is to inspect your server error log file. It will often tell you exactly what is wrong.

In this case, I would guess that you'll see a message indicating that the maximum redirection limit has been reached, and that the server is giving up. The reason for this is that you likely have a 'infinite' redirect/rewrite loop -- created by the interaction of mod_dir's DirectoryIndex directive and your new redirect function. This occurs because, when example.com/index.html is requested by a client:

1) Your Redirect directive redirects all requests for the URL "example.com/index.html" to the URL "example.com/"
2) The DirectoryIndex directive rewrites the request for the URL "/" to a request for the file "/index.html"
3) Now it's back to step one, and now you've got a redirect/rewrite loop, which will continue until the client or server gives up.

The solution is to redirect only direct client requests for "/index.html" to "example.com/", using mod_rewrite's RewriteCond and RewriteRule directives:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.html([?#][^\ ]*)?\ HTTP/
RewriteRule ^index\.html$ http://www.example.com/ [R=301,L]

If you wish to redirect requests for "/index.html" in *any* subdirectory to "/" in that same subdirectory, then a slight tweak is needed:

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.html([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]

By checking that the HTTP request received from the client (and shown in your server access log) contains "/index.html", we ensure that only direct client requests for "/index.html" get redirected; Requests which have been previously internally rewritten from "/" to "/index.html" by mod_dir will NOT be redirected, thus preventing the 'infinite' loop.

Jim