2013-03-15 87 views
0

我正在使用mod_rewrite从文件中删除.HTML扩展名,并将其重定向到其友好版本。文件扩展名mod_rewrite会干扰验证文件吗?

我使用pinterest.htmlgoogle.html文件来验证我的Pinterest页面和Google Apps帐户。

mod_rewrite的,我用这:

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.html -f 
RewriteRule ^(.+)$ $1.html [L,QSA] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/ 
RewriteRule ^(.*)\.html$ /$1 [R=301,L] 

我的问题是关于相对于验证文件的逻辑和它的功能。我理解的目的是为'x' file'y' URL服务。

所以即使我服务example.com/pinterest.htmlexample.com/pinterest由于重定向和.HTML去除,不应该有任何干扰,因为该文件仍然是可读一如既往,正确吗?

回答

1

不要重写这两个特殊文件。向cond正则表达式添加否定断言:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(?!pinterest\.html|google\.html).*\.html\ HTTP/ 

PS。为何不仅匹配%{REQUEST_URI}

RewriteCond %{REQUEST_URI} !(pinterest|google)\.html$ 
RewriteCond %{REQUEST_URI} \.html$ 

我已经用简单的正则表达式重写了这个。这说的是不匹配两个特殊文件,但匹配.html文件的任何其他请求。

+0

谢谢你的解决方案,我不是很熟悉mod_rewrite,它只是我从另一个stackoverflow问题的答案中使用的代码。介意我问别的东西吗?在cond中,最后的'HTTP /'的目的是什么?如果我从FILENAME切换到URI,具体的区别是什么? – popsicle 2013-03-15 19:54:12

+1

查看更改:-) – TerryE 2013-03-15 19:58:52

+0

再次感谢。尝试了一些不同的组合,只得到了以下工作:! '的RewriteCond%{} REQUEST_FILENAME -f 的RewriteCond%{} REQUEST_FILENAME -d 的RewriteCond%{} REQUEST_FILENAME的.html -f 重写规则^(+)$ $ 1 .html [L,QSA] RewriteCond%{THE_REQUEST}^[AZ] {3,9} \ /(?! pinterest \ .html | google \ .html)。* \。html \ HTTP/ RewriteRule^。*)\。html $/$ 1 [R = 301,L]' 我正在阅读apache.org上'REQUEST_URI','REQUEST_FILENAME'和'THE_REQUEST'之间的差异。 'REQUEST_URI'似乎是最好的,但我不知道如何实现它。为了获得最佳性能,我可以在以上重写中更改哪些内容? – popsicle 2013-03-16 12:17:32