Mod重写仅适用于某些页面

时间:2015-04-26 18:34:56

标签: apache .htaccess mod-rewrite

我试图删除文件中的.php扩展名。 我有一个简单的网站,其中有几个页面在本地运行,并且有一个简单的htaccess规则 - 大多数页面都能正常工作但有两个不能正常工作并且无论如何都会继续显示.php。我没有任何与这些页面同名的文件夹。我的html链接到.php文件(例如contact.php),如果我不需要,我也不想改变它。

对于这两个例子,我都输入了网址(包括.php),所以我不知道为什么没有php和联系人就开始了。通过规则2x也是正常的吗? 感谢

htaccess的:

DirectoryIndex index.php

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L,QSA]

我的apache错误日志(为空间编辑,如果我删除任何有用的内容,请告诉我)

[rewrite:trace3] strip per-dir prefix: C:/wamp/www/test.site/about-us -> about-us
[rewrite:trace3] applying pattern '^(.*)$' to uri 'about-us'
[rewrite:trace4] RewriteCond: input='C:/wamp/www/test.site/about-us' pattern='!-d' => matched
[rewrite:trace4] RewriteCond: input='C:/wamp/www/test.site/about-us.php' pattern='-f' => matched
[rewrite:trace2] rewrite 'about-us' -> 'about-us.php'
[rewrite:trace3] add per-dir prefix: about-us.php -> C:/wamp/www/test.site/about-us.php
[rewrite:trace2] strip document_root prefix: C:/wamp/www/test.site/about-us.php -> /about-us.php
[rewrite:trace1] internal redirect with /about-us.php [INTERNAL REDIRECT]
[rewrite:trace3] strip per-dir prefix: C:/wamp/www/test.site/about-us.php -> about-us.php
[rewrite:trace3] applying pattern '^(.*)$' to uri 'about-us.php'
[rewrite:trace4] RewriteCond: input='C:/wamp/www/test.site/about-us.php' pattern='!-d' => matched
[rewrite:trace4] RewriteCond: input='C:/wamp/www/test.site/about-us.php.php' pattern='-f' => not-matched
[rewrite:trace1] pass through C:/wamp/www/test.site/about-us.php

[rewrite:trace3] strip per-dir prefix: C:/wamp/www/test.site/contact.php -> contact.php
[rewrite:trace3] applying pattern '^(.*)$' to uri 'contact.php'
[rewrite:trace4] RewriteCond: input='C:/wamp/www/test.site/contact.php' pattern='!-d' => matched
[rewrite:trace4] RewriteCond: input='C:/wamp/www/test.site/contact.php.php' pattern='-f' => not-matched
[rewrite:trace1] pass through C:/wamp/www/test.site/contact.php

1 个答案:

答案 0 :(得分:0)

确保您没有在.php中输入文件名,或者在您的html或带有php扩展名的表单中有这样的链接。

您可以做的是检查完整的文件名,以及是否重定向到无扩展名的URI。

我做了一些更改,您可以尝试使用此代码。

Options -MultiViews
DirectoryIndex index.php

RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^&\ ]+).php
RewriteRule .* /%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ $1.php [NC,L,QSA]

因此,即使您输入/contact.php,它也会重定向回/contact