【问题标题】:Mod rewrite rule doesn't workMod重写规则不起作用
【发布时间】:2012-09-14 15:06:21
【问题描述】:

我正在使用 mod rewrite,但我的代码不起作用。它工作了一段时间。 我已经剥离了代码。

.htaccess

RewriteEngine On 
RewriteBase / 
RewriteRule ^/(.*)/$ index.php?test=$1

PHP

<?php
var_dump($_GET['test']);
?>

如果我转到index.php,它会显示NULL

我不明白为什么它不再起作用了。我希望你能帮助我。

附:我已经通过使用 .htaccess 制作登录表单来测试是否加载了 .htaccess 文件。

【问题讨论】:

    标签: php .htaccess mod-rewrite rule


    【解决方案1】:

    你需要去掉第一个斜线:

    RewriteRule ^(.*)/$ index.php?test=$1
    

    即使这样,您的规则也只会在您输入以正斜杠结尾的网址时适用,例如:

    /index.php/
    

    如果您希望它与任何 url 一起使用,您还需要删除最后一个斜杠:

    RewriteRule ^(.*)$ index.php?test=$1
    

    编辑:为避免重写现有文件和目录,您需要添加一些条件:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?test=$1
    

    【讨论】:

    • 谢谢,这成功了!但我还有一个问题。在我的 index.php 文件中,我有一个指向样式表的链接,但该链接也被重写了。即使我使用绝对 URL。
    【解决方案2】:

    您需要删除规则中的第一个斜线

    RewriteEngine On 
    RewriteBase / 
    RewriteRule ^(.*)/$ index.php?test=$1
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 2015-07-26
      • 2015-01-06
      • 2010-11-13
      • 2012-05-13
      • 2018-04-05
      • 2012-06-14
      • 1970-01-01
      • 2013-07-06
      相关资源
      最近更新 更多