Forum Moderators: phranque

Message Too Old, No Replies

Order of rewrites and redirects

remapping site

         

willybfriendly

12:43 am on Jan 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am somewhat (OK, a whole lot) confused about the proper ordering of redirects and rewrites. I am in the middle of remapping an entire site from an old URL to a new one, and in the process have some specific rewrites related to the CMS I am using.

It is my understanding that we go from most general to most specific, but I want to be sure I have this right.

So...

#Rewrite will be used more later, but
RewriteEngine On
# We start by adding this to redirect oldsite.com to newsite, using the rule of
# most general to most specific. Also can add any other URLS that need to be
# redirecton on this server - correct?

RewriteRule ^(.*)$ [newsite.com...] [R=301,L]

#Now we map the old pages to the new, one by one

redirect 301 old-page.php [newsite.com...]
redirect 301 old-page.php [newsite.com...]

#etc....

# Now specific rewrites for the CMS

RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR] ##optional - see notes##
RewriteCond %{REQUEST_URI} (/¦\.htm¦\.php¦\.html¦/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) index.php

Unfortunately, the above gives me an internal server error. Clues as to what I am doing wrong?

g1smd

1:51 am on Jan 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do the most specific stuff first, otherwise you end up with a redirection chain.

Never redirect to a named index filename. You want to redirect to a folder-like URL ending with a trailing slash.

g1smd

1:54 am on Jan 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>> "component/option,com" <<

You're messing with a Joomla CMS site aren't you?

I hate Joomla as it is absolutely riddled with difficult duplicate content issues.

phranque

2:20 am on Jan 13, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



Unfortunately, the above gives me an internal server error. Clues as to what I am doing wrong?

there should be clues in the server error log...

willybfriendly

7:31 am on Jan 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a typo, which has solved the first problem.

Now I seem to be caught in an endless loop, with no error thrown. :(

Enough for tonight. Will have back at it in the morning.

jdMorgan

3:34 pm on Jan 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stop endless loop:

# If request to non-canonical domain, redirect to canonical domain
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Put your external redirect rules (R=301) first, in order from most-specific to least-specific. That is, put per-page redirects first, then per-directory redirects, and finally, per-server (domain) redirects. In general, the redirect rule above should be the last external redirect rule in your .htaccess file.

Follow these external redirect rules with your internal rewrites, again from most-specific to least specific, as outlined above. Your 'Joomla' rewrite should be the last (or one of the last) rewrites in your .htaccess file.

[added] Also, for best results, you'll want to enforce the order of execution of your rules. To allow this, do not mix directives from different modules. That is, use mod_rewrite or mod_alias, not both as you've done above. Redirect is a mod_alias directive, while RewriteRule is handled by mod_rewrite. It is the server configuration which decides whether mod_alias or mod_rewrite directives are processed first, and you cannot control that by ordering the directives in your .htaccess file. In other words, the server will process all mod_alias directives first, followed by all mod_rewrite directives, or vice-versa, depending on how it's configured. So in order to enforce order of execution while using advanced features like "check requested host name" and "check for file exists" as you have done, you'll have to use mod_rewrite only, because mod_alias does not support those features. [/added]

Jim

[edited by: jdMorgan at 3:41 pm (utc) on Jan. 13, 2008]

willybfriendly

11:04 pm on Jan 13, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim,

I just want to thank you for your patience and the depth of your responses in this forum. I am always impressed by how well you explain not just what to do, but why to do it. You are able to simplify a very complex subject and share it in a non-threatening way. A great teacher!

I have yet to return to the problem at hand, but I expect that your response will prove invaluable in solving the problem.

willybfriendly

11:42 pm on Jan 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Everything is now working. I will watch for 404's for a few days to see if I missed anything when I mapped it over.

Thanks to all...

g1smd

12:41 am on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It would probably be worth running Xenu LinkSleuth over at least part of the site, and just keeping an eye open for any issues whatsoever.

willybfriendly

5:19 am on Jan 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Xenu has been run several times. Most common agent showing in the logs as of yesterday.