【问题标题】:URL rewriting using old .htaccess rules使用旧的 .htaccess 规则重写 URL
【发布时间】: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/previewdomain.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.phpdomain.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。

【问题讨论】:

  • 首先,不要使用&amp;%{QUERY_STRING},只需将 [L] 更改为 [L,QSA] ,apache 将附加查询字符串,第二个正则表达式中的点是什么?您的意思是 ^share/([^/]+)/?$ 而不是 ^share/([^/\.]+)/?$ 吗?如果共享只有数值,您也可以^share/([0-9]+)/?$

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

在您的域配置中,启用了mod_negotiationMultiViews 选项。关于该选项的reference 可能会有所帮助。它完全按照您的描述以及您不希望发生的情况进行。对此的处理发生在评估重写规则之前,因此它可能与您的问题有关。

您可以通过从域配置中删除 MultiViews 选项或在您的 .htaccess 文件中添加以下行来禁用此行为:

Options -MultiViews

此外,您应该使用QSA 标志(用于查询字符串追加)而不是手动追加%{QUERY_STRING}

【讨论】:

  • 非常感谢!我准备把我的笔记本电脑扔出窗外!我将 Options -MultiViews 添加到域 conf 文件的 Directory 部分,并将 QUERY_STRING 替换为 QSA。现在一切都很好:)
猜你喜欢
  • 2011-12-06
  • 2016-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多