Can you see something wrong in my working .htaccess?
Posted
by
AlexV
on Server Fault
See other posts from Server Fault
or by AlexV
Published on 2011-01-07T16:47:32Z
Indexed on
2011/01/07
16:56 UTC
Read the original article
Hit count: 165
mod-rewrite
|.htaccess
OK, after many search, trial and errors I've managed to create an .htaccess that do what I wanted (see explanations and questions after the code block):
<IfModule mod_rewrite.c>
RewriteEngine On
#1 If the requested file is not url-mapper.php (to avoid .htaccess loop)
RewriteCond %{REQUEST_FILENAME} (?<!url-mapper\.php)$
#2 If the requested URI does not end with an extension OR if the URI ends with .php*
RewriteCond %{REQUEST_URI} !\.(.*) [OR]
RewriteCond %{REQUEST_URI} \.php.*$ [NC]
#3 If the requested URI is not in an excluded location
RewriteCond %{REQUEST_URI} !^/seo-urls\/(excluded1|excluded2)(/.*)?$
#Then serve the URI via the mapper
RewriteRule .* /seo-urls/url-mapper.php?uri=%{REQUEST_URI} [L,QSA]
</IfModule>
This is what the .htaccess should do:
- #1 is checking that the file requested is not url-mapper.php (to avoid infinite redirect loops). This file will always be at the root of the domain.
- #2 the .htaccess must only catch URLs that don't end with an extension (www.foo.com --> catch | www.foo.com/catch-me --> catch | www.foo.com/dont-catch.me --> don't catch) and URLs ending with .php* files (.php, .php4, .php5, .php123...).
- #3 some directories (and childs) can be excluded from the .htaccess (in this case /seo-urls/excluded1 and /seo-urls/excluded2).
- Finally the .htaccess feed the mapper with an hidden GET parameter named uri containing the requested uri.
Even if I tested and everything works, I want to know if what I do is correct (and if it's the "best" way to do it). I've learned a lot with this "project" but I still consider myself a beginner at .htaccess and regular expressions so I want to triple check it there before putting it in production...
© Server Fault or respective owner