【问题标题】:htaccess - removes index.php, but not index.php when index.php?z=abchtaccess - 删除 index.php,但当 index.php?z=abc 时不删除 index.php
【发布时间】:2012-01-05 22:31:26
【问题描述】:

http://domain.com/index.php -> http://domain.com/ - 好的

http://domain.com/index.php?z=abc -> http://domain.com/index.php?z=abc - 不正常,保持不变。 这可以使用以下代码:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/
RewriteRule ^(.*)index\.php$ /$1 [R=301,L]

如何修改这段代码,以便

http:// domain.com/index.php?z=abc -> http:// domain.com/?z=abc

【问题讨论】:

    标签: php .htaccess mod-rewrite indexing


    【解决方案1】:

    Apache 会自动处理查询字符串。你只需要这个:

    RewriteEngine on
    RewriteRule ^index.php / [L,R=301]
    

    【讨论】:

    • [R=301] 会中断 POST 请求(表单数据丢失),但不影响地址栏。出于这个原因,如果(曾经)发布到索引,您会希望避免进行这种重写。
    • @Umbrella 大多数服务器默认为 301,无论如何。而且由于他不希望/index.php 显示在地址栏中,我怀疑他会发帖。
    • @KristianAntonsen 一个合理的怀疑,但由于 OP 提出了一个关于这打破他的帖子的新问题,我认为这个提醒可能有助于其他人找到这个线程。
    【解决方案2】:

    试试下面修改后的 RewriteCond

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php(\?[^\ ]*)?\ HTTP/
    RewriteRule ^(.*)index\.php$ /$1 [R=301,L]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-06
      • 2016-05-13
      • 2016-03-15
      • 2013-12-14
      • 2013-09-15
      • 2011-05-20
      • 2012-08-15
      • 2015-03-09
      相关资源
      最近更新 更多