【发布时间】:2014-05-18 21:31:15
【问题描述】:
我在重写 URL 时遇到了一些实际问题,我们将不胜感激。
我有一个名为 share.php 的 php 文件和一个包含以下规则的 .htaccess 文件:
RewriteRule ^share/([^/\.]+)/?$ share.php?variable=$1&%{QUERY_STRING} [L]
如果我导航到domain.com/share/123456,则会加载share.php 页面,但$_GET['variable'] 中没有值。
我在 .htaccess 文件中有一些其他的重写规则,我认为它们可能有问题,所以我删除了所有其他规则。我删除的规则之一是 rewriting domain.com/preview 到 domain.com/preview.php
RewriteRule ^preview/?$ preview.php?&%{QUERY_STRING} [L]
然后我注意到,即使删除了所有其他规则,如果我导航到 domain.com/preview,它仍然会将 URL 重写为 preview.php。
所以,我完全删除了 .htaccess 文件,重新启动了 Apache,清除了 Firefox 中的缓存并再次对其进行了测试。即使 .htaccess 文件已被删除,规则仍然有效。
domain.com/preview 仍然重定向到 domain.com/preview.php 和 domain.com/share/123456 仍然重定向到 domain.com/share.php($_GET['variable'] 中没有数据)。
2 个域指向服务器,但任何 .conf 文件中都没有重写规则,并且不再有任何 .htaccess 文件。我已经尝试将重写规则放在域的 .conf 文件中,但我遇到了同样的问题。
如果我运行phpinfo(),Modrewrite 将显示为已加载模块。这是我的域的 .conf 文件:
<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /home/www/domain.com/public_html
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory /home/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
AllowOverride All
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
Alias /doc/ "/usr/share/doc/"
<Directory "/usr/share/doc/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Require all denied
Allow from 127.0.0.0/255.0.0.0 ::1/128
</Directory>
</VirtualHost>
我整个下午都在试图解决这个问题,但我就是看不出有什么问题!谁能指出我正确的方向?
我真的不知道从哪里开始解决这个问题。我打开了重写日志记录,但日志文件中没有任何内容。
我在 Ubuntu 12.04 上使用 PHP 5.5.10 运行 Apache 2.4.9。
【问题讨论】:
-
首先,不要使用
&%{QUERY_STRING},只需将 [L] 更改为 [L,QSA] ,apache 将附加查询字符串,第二个正则表达式中的点是什么?您的意思是^share/([^/]+)/?$而不是^share/([^/\.]+)/?$吗?如果共享只有数值,您也可以^share/([0-9]+)/?$
标签: php apache .htaccess mod-rewrite url-rewriting