Forum Moderators: phranque

Message Too Old, No Replies

What order for .htaccess RewriteRules?

         

mayest

8:41 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I'm pretty new to rewrites, but I have successfully done several of them during my site redesign. My question is about the correct order of rewrite rules in the .htaccess file. I'm pretty sure that I saw a post from jdMorgan not long ago that said the rules should be listed either from most specific to least specific, or the other way around. Unfortunately, I can't find that post.

Here is what I have at the moment (in the order listed), and it seems to work, but I don't want to cause trouble down the road:

1) rewrite to canonical URL
2) rewrite to remove "index.php?" from the URLs created by my new CMS.
3) several rewrites for specific files to their new locations
4) a couple of AddHandler statements to force *.php files to run under PHP 5 instead of PHP 4.

Is that the correct order, or am I asking for trouble?

Thanks,

Tim

jdMorgan

9:04 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried a search [google.com] using the words in your question below? :)

Only the order of directives belonging to the same module (e.g mod_rewrite) are of any consequence; The server configuration specifies which modules will process your .htaccess file, and in what order they will do so. As a result, it's generally best to group all directives belonging to the same module together, just to keep them organized.

For more info, see the Apache documentation for the LoadModule directive.

Jim

mayest

10:50 pm on Dec 20, 2007 (gmt 0)

10+ Year Member



I suggest putting your script rewriting rules in order from most-specific to least specific, and using unambiguous regex patterns wherever possible -- and that means "most of the time." -- In addition to preventing unexpected matches and the resulting problems, they also offer a very-significant processing-speed advantage in many cases.

Thanks Jim. I found the above quote. So, the very last thing should be the URL canonicalization. Right?

jdMorgan

11:06 pm on Dec 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure, since you used the word "rewrite" for all of the steps. If you put an external redirect after an internal rewrite, then the external redirect will 'expose' the rewritten URL to the client (browser or robot) -- probably not what you want.

Jim

g1smd

12:35 am on Dec 22, 2007 (gmt 0)

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



I use this order:

- redirect all URLs in parameter-based formats to folder-based format at www of correct domain (and with no parameters in target).
- redirect named index files to "/" at www, preserving folder names, and strip parameters.
- redirect non-www and www on other domains to www on correct domain, preserve filepath and filename, and strip parameters.
- redirect non-www on this domain to www on this domain, preserve filepath and filename, and strip parameters.
- redirect URL. Strip parameters from www URL, leave rest of URL intact.
- internally rewrite folder based URLs to internal filenames with attached parameters. These are *NOT* URLs.

Each rule has [L] at the end.

g1smd

12:54 am on Dec 22, 2007 (gmt 0)

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



The processing order in your original post would cause a redirection chain.

Trace what happens for a non-www index file. It gets redirected to www and then it gets redirected again to remove the filename.

See my post above for one better way to do it. There is no single one right way, but there are very many incorrect ways to do it.

mayest

8:15 pm on Dec 22, 2007 (gmt 0)

10+ Year Member



Thanks g1smd for the detail on the order. I'll change it around and see what happens.