【问题标题】:cannot redirect querystring url无法重定向查询字符串 url
【发布时间】:2018-05-28 05:28:25
【问题描述】:

我正在尝试清理 url 重定向查询字符串 URL。我已经尝试了 stackoverflow 和其他站点中提供的所有答案和规则,但没有任何改变。我以为是 .htaccess 文件错误,但我发现它可以正常生成其他错误。我正在尝试重定向

http://localhost/ourallnews/category.php?cat=News

http://localhost/ourallnews/News/

首先我尝试使用它。

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=(\w+)
RewriteRule ^ourallnews/category\.php$ /ourallnews/%1? [R=302,L]

后来我尝试了

RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat=(\w+)$
RewriteRule ^ourallnews.*$ /ourallnews/%1/? [R=302,L]

没有任何改变。

我清除浏览器缓存,重新启动 apache 但不起作用。查询字符串 url 被保留。

那么最后这个带来了改变。

RewriteCond %{QUERY_STRING} .
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

改变

http://localhost/ourallnews/category.php?cat=News

http://localhost/ourallnews/category.php

但我的要求是显示查询字符串值而不是 URI。

http://localhost/ourallnews/news/

请帮帮我

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    你可以用这个

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^cat=(\w+)
    RewriteRule ^ourallnews/category\.php$ /ourallnews/%1? [R=302,L]
    

    这会将/ourallnews/category.php?cat=value 重定向到/ourallnews/value

    【讨论】:

    • 我试过这个,但没有任何改变。除了你的答案,我尝试了stackoverflow中提供的所有答案,但没有运气。这个RewriteCond %{QUERY_STRING} . RewriteRule ^ %{REQUEST_URI}? [R=301,L]http://localhost/ourallnews/category.php?cat=News 更改为http://localhost/ourallnews/category.php 但无法满足我的要求。请参阅更新的问题。感谢您的合作。
    • @AnandHmt 你要去什么网址,你会遇到什么错误?
    • 有没有办法显示 htaccess 错误日志?在 htaccess 中添加这一行时,没有任何变化。我的意思是当访问http://localhost/ourallnews/category.php?cat=News 链接时,它会在http://localhost/ourallnews/news/ 中自动更改。但它没有改变,仍然保留与查询字符串相同的 URL。
    • 这条规则在我的服务器上运行良好。我认为您已将其放置在您的 htaccess 中的某个位置,它不会被其他规则调用或覆盖。将它放在 htaccess 的顶部(在其他指令之前)并清除浏览器缓存以对其进行测试。
    • 我添加了顶部,清除缓存,删除并重新创建 .htaccess 只添加了这 3 行,仍然没有运气,html/php 编码结构会影响 htaccess 规则吗?我认为问题出在 php/html 文件
    【解决方案2】:

    以下对我有用:

    RewriteEngine on
    RewriteCond %{QUERY_STRING} ^cat=(\w+)$
    RewriteRule ^ourallnews.*$ /ourallnews/%1/? [R=302,L]
    

    与你的一些不同:

    RewriteRule 匹配:

    • ^ourallnews.*$ 将匹配以 ourallnews 开头的任何内容
    • ^ourallnews/$ 不匹配,因为$ 表示没有category.php 的字符串结尾

    RewriteRule重写:

    • /ourallnews/%1/? 是您想要的输出 URL
    • 重写结果中无需映射到category.php

    【讨论】:

    • 我试过这个,但没有任何改变。除了你的答案,我尝试了stackoverflow中提供的所有答案,但没有运气。这个RewriteCond %{QUERY_STRING} . RewriteRule ^ %{REQUEST_URI}? [R=301,L]http://localhost/ourallnews/category.php?cat=News 更改为http://localhost/ourallnews/category.php 但无法满足我的要求。请参阅更新的问题。感谢您的合作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 2018-06-04
    • 2012-11-29
    • 2021-04-05
    相关资源
    最近更新 更多