好吧,经过许多search,试用和错误,我已经设法创build一个.htaccess做我想做的事情(请参阅代码块后的解释和问题):
<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>
这是.htaccess应该做的事情:
即使我testing了一切,我想知道我所做的是否正确(如果是“最好”的方法)。 我从这个“项目”中学到了很多东西,但是我仍然认为自己是.htaccess和正则expression式的初学者,所以我想在把它放入生产环境之前再进行三重检查。
2a将允许/some.dir/file,因为“。” 在中间。 你可能需要像!\..[1,3]这样的东西,这取决于你的文件/目录结构。
在RewriteRules中,使用replace匹配而不是variables名是正常的:
RewriteRule (.*) /seo-urls/url-mapper.php?uri=$5 [L,QSA]
注意它是$5因为你现在有一个符合你的条件的匹配模式。