I'm having some trouble with mod_rewrite rules in a PHP site I'm doing.
I'm on Apache 2.4.9 (Unix) - apache that comes with Mac OSX Yosemite.
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
. Here's what I have in .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
:
<?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. But when I go to http://project.local/page/testpage
, it shows the Hello
but not the $_GET['var']
. What am I doing wrong?
Disable options MultiViews
by placing this code in your .htaccess:
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. So /file
can be in URL but it will serve /file.php
.
我通过在.htaccess
文件中添加以下行来解决此问题。
Options FollowSymLinks
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.