我在PHP网站上遇到了一些mod_rewrite规则的问题。
我正在使用Apache 2.4.9(Unix) - Mac OSX Yosemite附带的apache。
我在项目的根目录中有.htaccess
文件,并且它设置为在虚拟主机中运行...所以我到达项目的地址是http://project.local
。这是我在.htaccess
中的内容:
RewriteEngine On
# Don't rewrite files
RewriteCond %{REQUEST_FILENAME} !-f
# Don't rewrite directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pages pages.php [NC]
RewriteRule ^page/([0-9a-zA-Z]+) page.php?var=$1 [QSA]
这是我的虚拟主机设置:
<VirtualHost *:80>
ServerAdmin me@me.com
DocumentRoot "/Users/Me/Sites/project"
ServerName project.local
ServerAlias project.local
ErrorLog "/Users/Me/Sites/logs/project_error_log"
</VirtualHost>
以下是page.php
中的内容:
<?php
echo "Hello " . $_GET['var'];
?>
我可以成功访问http://project.local/pages
,并显示上述规则中pages.php
的内容。但是当我转到http://project.local/page/testpage
时,它会显示Hello
,但不显示$_GET['var']
。我究竟做错了什么?
答案 0 :(得分:0)
将此代码放入.htaccess:
,以禁用选项MultiViews
Options -MultiViews
RewriteEngine On
# Don't rewrite files
RewriteCond %{REQUEST_FILENAME} !-f
# Don't rewrite directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pages pages.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9a-zA-Z]+)/?$ page.php?var=$1 [QSA,L,NC]
MultiViews
使用选项Apache's content negotiation module
在 mod_rewrite
之前运行,并使Apache服务器匹配文件扩展名。因此/file
可以在网址中,但它会投放/file.php
。
答案 1 :(得分:0)
我通过在.htaccess
文件中添加以下行来修复此问题。
Options FollowSymLinks