Apache - 使用httpd.conf中的mod_rewrite删除.php和.html文件扩展名
问题描述:
在Ubuntu 14.04上运行Apache 2。 rewrite_module已启用(sudo apachectl -M
)。Apache - 使用httpd.conf中的mod_rewrite删除.php和.html文件扩展名
在/etc/apache2/apache2.conf中(httpd.conf中的Ubuntu的版本)我有以下代码块:
<Directory /var/www/>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
RewriteCond /%{REQUEST_FILENAME}.html -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.html
</IfModule>
<IfModule mod_expires.c>
...
<IfModule mod_headers.c>
...
</IfModule>
</IfModule>
</Directory>
然sudo service apache2 restart
。
当我访问我的服务器上没有.php文件扩展名的URL时,我得到了404!为什么这不起作用?
答
我终于弄明白了。这对我有效的是:
<Directory /var/www/>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [L]
</IfModule>
<IfModule mod_expires.c>
...
<IfModule mod_headers.c>
...
</IfModule>
</IfModule>
</Directory>
答
您的规则适用于我的htaccess上下文。但是当我将这些规则添加到serverConfig上下文服务器返回404找不到。其实问题是,在serverConfig上下文RewriteRule模式中的前导斜杠是必需的。所以你需要在你的Rule模式中加上一个斜杠:
RewriteRule ^/([a-zA-Z0-9_-\s]+)/?$ /$1.php
对不起,那也没有工作。 :( – torjinx