你能在我的.htaccess中看到错误吗?

好吧,经过许多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应该做的事情:

  1. 1正在检查所请求的文件不是url-mapper.php(以避免无限redirect循环)。 该文件将始终位于域的根目录下。

  2. 2 .htaccess只能捕获不以扩展名结尾的URL(www.foo.com – > catch | www.foo.com/catch-me – > catch | www.foo.com/dont-catch .me – >不捕捉) 以.php *文件(.php,.php4,.php5,.php123 …)结尾的URL。

  3. 3一些目录(和孩子)可以排除在.htaccess(在这种情况下/ seo-urls / excluded1和/ seo-urls / excluded2)。

  4. 最后,.htaccess为mapper提供一个名为uri隐藏 GET参数,其中包含请求的uri。

即使我testing了一切,我想知道我所做的是否正确(如果是“最好”的方法)。 我从这个“项目”中学到了很多东西,但是我仍然认为自己是.htaccess和正则expression式的初学者,所以我想在把它放入生产环境之前再进行三重检查。

2a将允许/some.dir/file,因为“。” 在中间。 你可能需要像!\..[1,3]这样的东西,这取决于你的文件/目录结构。

在RewriteRules中,使用replace匹配而不是variables名是正常的:
RewriteRule (.*) /seo-urls/url-mapper.php?uri=$5 [L,QSA]
注意它是$5因为你现在有一个符合你的条件的匹配模式。