mod_rewrite隐藏.php扩展名并删除其他标题

时间:2014-01-01 19:32:45

标签: php apache mod-rewrite

我想在网址和Apache的回复中隐藏php扩展

我尝试使用以下tipycal配置:

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

DirectoryIndex index.php index.html

DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
    Options -Indexes -MultiViews FollowSymLinks
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>
...

但我不明白为什么Apache添加了一组我认为不必要的标题。 例如关于这个网址:

http://mysite/page

修改 使用-MultiViews禁用内容协商会导致:

# tailf /var/log/apache2/error.log
[Sun Jan 19 16:35:23 2014] [error] [client 127.0.0.1] Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace.
[Sun Jan 19 16:35:23 2014] [debug] core.c(3063): [client 127.0.0.1] r->uri = /var/www/index.php
[Sun Jan 19 16:35:23 2014] [debug] core.c(3069): [client 127.0.0.1] redirected from r->uri = /var/www/index.php
...
[Sun Jan 19 16:35:23 2014] [debug] core.c(3069): [client 127.0.0.1] redirected from r->uri = /var/www/index.php
[Sun Jan 19 16:35:23 2014] [debug] core.c(3069): [client 127.0.0.1] redirected from r->uri = /page
[Sun Jan 19 16:35:23 2014] [debug] mod_deflate.c(615): [client 127.0.0.1] Zlib: Compressed 533 to 322 : URL /var/www/index.php


# tailf /var/log/apache2/rewrite.log 
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed69340/initial] (1) pass through /page
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed69340/initial] (1) [perdir /var/www/] internal redirect with /var/www/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed75738/initial/redir#1] (1) pass through /var/www/index.php
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed75738/initial/redir#1] (1) [perdir /var/www/] internal redirect with /var/www/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [19/Jan/2014:18:42:06 +0100] [mysite/sid#7fa89ea62d58][rid#7fa89ed77ad0/initial/redir#2] (1) pass through /var/www/index.php
...

3 个答案:

答案 0 :(得分:2)

您认为这是负责重命名的mod_rewrite,但它实际上是mod_negotiation,更具体地说是Multiviews选项,因为显然mod_negotiation已执行早于Apache 2.0及更高版本的请求生命周期中的mod_rewrite

正在发生的事情是,当请求http://mysite/page并且相应目录中不存在page文件时,mod_negotiation将尝试查找模式为page.*的文件。
如果确实存在名为page.php的文件,那么mod_negotiation将告诉Apache处理当前请求,就像请求http://mysite/page.php一样,并设置一些响应头,主要用于帮助客户端缓存:

Vary: negotiate

此请求正在使用内容协商。

TCN: choice

这意味着资源是透明协商的,选择意味着返回了“最佳”变体,变体的原始名称出现在Content-Location标题中。

Content-Location: page.php

此设置是为了让浏览器知道page.phppage的最佳变体。

因此,您的案例中的解决方案是通过确保来禁用内容协商  Options -Multiviews
存在,或者确保未加载mod_negotiation,因此您可以确定mod_rewrite将处理重命名,而不添加这些标题。

答案 1 :(得分:0)

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

DocumentRoot /var/www/mysite
<Directory /var/www/mysite>
Options -Indexes -MultiViews FollowSymLinks
...

答案 2 :(得分:-1)

试试这段代码(但是php和非php标签都可以使用它,除非你让它重定向它们)

RewriteEngine On
RewruteRule ^(.*)$ /$1.php

有关详细信息,请查看此链接 - http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

相关问题