【问题标题】:Redirecting /index.php?country=a to /a/ by htaccess通过 htaccess 将 /index.php?country=a 重定向到 /a/
【发布时间】:2016-07-26 10:24:40
【问题描述】:

重定向

www.example.com/index.php?country=a

www.example.com/a/

试过了:

RewriteCond %{QUERY_STRING} index.php?country=a [OR]
RewriteCond %{REQUEST_URI} index.php?country=a
RewriteRule ^ http://www.example.com/a/? [L,R=301]

不起作用。

注意事项:

1) 如果 a 是动态的(我的列表中有 60 个国家/地区),此代码也是一个坏主意。

2) index.php?country=a 之后的任何内容,例如:index.php?country=a&p=d&xx=k 将被删除。

【问题讨论】:

  • 你可能想做相反的重定向吗?为什么要将“标准”url 类型重定向到 SEO 类型?
  • 可能你错过了我的第二条笔记。我不是在寻找干净的 URL。试图删除一堆禁用的 URL。
  • 您也可以在 PHP 应用程序中执行此操作。而不是返回(旧)页面,只需返回 header('Location: http://example.com/country');

标签: php .htaccess url clean-urls


【解决方案1】:
RewriteRule ^a$ index.php?country=a [NC,L]

这个肯定有效...确保 index.php?country=a 不在任何子目录中

【讨论】:

    【解决方案2】:

    变量%{QUERY_STRING}代表url的查询部分(?后面的部分),

    所以你不能匹配模式中的uri index.php,试试:

    RewriteCond %{QUERY_STRING} ^country=([^&]+)(?:&(.*))?$ [NC]
    RewriteRule ^ http://www.example.com/%1/? [L,R=301]
    

    或者

    RewriteCond %{THE_REQUEST} /index\.php\?country=([^&]+)
    RewriteRule ^ http://www.example.com/%1/? [L,R=301]
    

    在测试这些规则之前清除浏览器的缓存。

    【讨论】:

    • 那么有什么方法可以匹配'index.php'?
    • 查看第二个解决方案
    • 谢谢,但您看过有问题的笔记了吗? 'a' 在这里是动态的
    • @smith 是的,我做到了!您想将 /index.php?country=foo 形式的传入 url 重定向到 /foo/ 吗?
    【解决方案3】:
    RewriteRule ^/([a-zA-Z]+) index.php?country=$1 [NC,L]
    

    它将帮助您从任何值中获得一个干净的 URL,而不仅仅是 'a'

    【讨论】:

    • 它也忽略了国家之后的其他值
    • 有重写条件吗?
    猜你喜欢
    • 2013-01-21
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-31
    • 1970-01-01
    • 2011-12-13
    • 2014-10-17
    • 2012-04-27
    相关资源
    最近更新 更多