[英]Mod_rewrite rules for project in .htaccess
I'm having some trouble with mod_rewrite rules in a PHP site I'm doing. 我在执行的PHP网站中使用mod_rewrite规则遇到麻烦。
I'm on Apache 2.4.9 (Unix) - apache that comes with Mac OSX Yosemite. 我使用的是Apache 2.4.9(Unix)-Mac OSX Yosemite随附的apache。
I have the .htaccess
file in the root of the project, and it's set up to run in a virtual host... so the address I'm reaching the project from is http://project.local
. 我在项目的根目录中有
.htaccess
文件,并且将其设置为在虚拟主机中运行...因此,我要从中访问项目的地址是http://project.local
。 Here's what I have in .htaccess
: 这是我在
.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]
Here's my virtual host set-up: 这是我的虚拟主机设置:
<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>
And here is what I have in page.php
: 这是我在
page.php
中page.php
:
<?php
echo "Hello " . $_GET['var'];
?>
I can access http://project.local/pages
successfully, and it shows the content from pages.php
from the above rule. 我可以成功访问
http://project.local/pages
,它显示了上述规则中pages.php
的内容。 But when I go to http://project.local/page/testpage
, it shows the Hello
but not the $_GET['var']
. 但是当我转到
http://project.local/page/testpage
,它显示的是Hello
而不是$_GET['var']
。 What am I doing wrong? 我究竟做错了什么?
Disable options MultiViews
by placing this code in your .htaccess: 通过将以下代码放在您的.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]
Option MultiViews
is used by Apache's content negotiation module
that runs before mod_rewrite
and makes Apache server match extensions of files. Apache's content negotiation module
使用Option MultiViews
,该Apache's content negotiation module
在 mod_rewrite
之前运行,并使Apache服务器匹配文件扩展名。 So /file
can be in URL but it will serve /file.php
. 因此
/file
可以位于URL中,但它将提供/file.php
。
我通过在.htaccess
文件中添加以下行来解决此问题。
Options FollowSymLinks
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.