【问题标题】:URL rewriting match is always "index.php"URL 重写匹配始终是“index.php”
【发布时间】:2014-05-03 20:06:49
【问题描述】:

我正在尝试设置 RewriteEngine 以便它可以重定向:

http://www.url.com/some/parameter

http://www.url.com/index.php?page=some&topic=parameter

或类似的东西。

我的 .htaccess 文件如下所示:

Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^([^/]+)(?:/([^/]+))*/?$ index.php?param1=$1&param2=$2 [L]

当我现在访问http://www.myurl.com/test/param 时,它没有正确添加参数(或者根本不工作)。 当我打印所有 $_GET 条目时,它总是说:

param1 -> index.php
param2 -> 

//Code:
foreach($_GET as $k=>$v)
    print($k . ' -> ' . $v . '<br/>');

我希望有人有想法。

编辑: 我只是注意到,如果我这样写:http://www.myurl.com/index.php/param1/param2,它会起作用,但我不希望该 URL 中有 index.php。

【问题讨论】:

    标签: php regex apache mod-rewrite redirect


    【解决方案1】:

    您需要使用RewriteCond 来阻止此规则第二次执行:

    Options +FollowSymLinks
    Options +Indexes
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)(?:/([^/]+))*/?$ index.php?param1=$1&param2=$2 [L,QSA]
    

    如果没有这 2 个 RewriteCond,您的规则将第二次执行,因为模式仍然匹配 /index.php URI。

    【讨论】:

    • 谢谢,它有效。我如何将myurl.com 重定向到myurl.com/index.php?page=home
    • 不客气。我建议为此创建一个新问题,因为这与当前的问题非常不同,并且由于格式问题,建议在 cmets 中编写代码并不容易。
    • 我会用谷歌搜索,看看是否找到适合我的东西,如果没有帮助,我会创建一个新问题。
    • 是的,当然,当您创建新问题时,请随时在此处发表评论。
    【解决方案2】:

    对于[L],您还需要指定[QSA],表示“查询字符串追加”。

    RewriteRule ^([^/]+)(?:/([^/]+))*/?$ index.php?param1=$1&param2=$2 [L,QSA]
    

    http://httpd.apache.org/docs/2.2/rewrite/flags.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-19
      • 2013-02-13
      • 1970-01-01
      相关资源
      最近更新 更多