Order in .htaccess?

New to mod_rewrite? This is a good place to start.

Order in .htaccess?

Postby hm78 » Thu Jun 04, 2009 7:40 am

I wonder, if it matters, in which order ".htaccess blocks" (don't know the correct term) appear in the .htaccess file?

What I need in my .htaccess:

encoding (or is it charsets?)
Code: Select all
AddCharset utf-8 .html .css .rdf .txt .xml .rss


error documents
Code: Select all
ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 410 /error/410.html


www. to non-www.
Code: Select all
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]


hiding of the .html extension
(see thread)

hand-crafted RedirectPermanent
(explicit / absolute URLs, for a few deleted/moved pages)

in detail:
should I enclose the www to non-www. redirection into the <IfModule mod_rewrite.c> block of the .html extension hiding? Or should I open a new block? Or should I write it without any enclosing block?

manual RedirectPermanent (like RedirectPermanent /about.html http://example.com/) before or after the www to non-www. / .html extension hiding blocks?

Options (like Options +FollowSymLinks -MultiViews) at the very top, or before it is needed by a block?

... and so on.
hm78
 
Posts: 17
Joined: Sun Mar 04, 2007 8:26 pm

Postby richardk » Thu Jun 04, 2009 1:31 pm

Only the order of the mod_rewrite and maybe Redirect* (mod_alias) should matter.

should I enclose the www to non-www. redirection into the <IfModule mod_rewrite.c> block of the .html extension hiding? Or should I open a new block? Or should I write it without any enclosing block?

The same <IfModule>.

Code: Select all
AddCharset utf-8 .html .css .rdf .txt .xml .rss
Options +FollowSymLinks -MultiViews

ErrorDocument 401 /error/401.html
ErrorDocument 403 /error/403.html
ErrorDocument 404 /error/404.html
ErrorDocument 410 /error/410.html

Redirect 301 /about.html http://example.com/

<IfModule mod_rewrite>
  DirectorySlash Off

  RewriteEngine On

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

  RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\  [NC]
  RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]

  RewriteCond %{SCRIPT_FILENAME}/ -d
  RewriteCond %{SCRIPT_FILENAME}.html !-f
  RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

  RewriteCond %{ENV:REDIRECT_STATUS} ^$
  RewriteRule ^(.+)\.html$ /$1 [R=301,L]

  RewriteCond %{SCRIPT_FILENAME}.html -f
  RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
</IfModule>


You might have problems mixing mod_rewrite and mod_alias (Redirect*). If you do, you can do the redirects with mod_rewrite instead
Code: Select all
RewriteRule ^about\.html$ http://example.com/ [R=301,L]

instead of
Code: Select all
Redirect 301 /about.html http://example.com/
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 122 guests

cron