【问题标题】:htaccess redirect url query string to other string parameterhtaccess 将 url 查询字符串重定向到其他字符串参数
【发布时间】:2014-08-29 19:05:22
【问题描述】:

我已经让 htaccess 在查询字符串中使用 + 符号重定向空间 %20。然后我有另一个规则从一个目录重定向到另一个。

示例:

http://Mysite/domestic-jobs/?domestic=val or otherVal

将被重定向到

http://Mysite/jobs/?domestic=val+or+otherVal

现在我需要在我的 htaccess 文件中包含另一个规则,该规则将 ? 之后的查询字符串参数从 ?domestic= 重定向到 ?position=。如何在不中断文件中其他规则的情况下做到这一点?

示例:

http:/Mysite/domestic-jobs/?domestic=val or otherVal

我需要被重定向到

http:/site/jobs/?position=val+or+otherVal

这是我目前所拥有的。

Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)%20(.*)$
RewriteRule ^(.*)$ /$1?%1+%2 [L,R=301,NE]

RewriteRule ^domestic-jobs/(.*)$ /jobs/$1 [R=301,NC,L]

【问题讨论】:

  • Jon Lin 的回答奏效了。只需在我的 htaccess 文件中已经存在的第一个 Cond 和 Rule 之后添加 Cond 和 Rule。否则它没有正确重定向。

标签: apache .htaccess mod-rewrite redirect


【解决方案1】:

RewriteEngine On 下添加:

RewriteCond %{QUERY_STRING} ^domestic=(.*)$
RewriteRule ^(.*)$ /$1?position=%1 [L,R=301]

【讨论】:

  • 谢谢。如果我拥有的不仅仅是 ?domestic=.假设我有 ?blah= 和 ?blahblah= 和 ?blahblahblah= 并且想要全部更改为 ?position=。是否有通配符或我可以使用的东西会改变之后的任何内容?使用 ?position= 如果 ?position= 不在 ? 之后
  • domestic= 更改为[^=]+=
  • 嗯,它导致发生无限循环。只有在不是?position= 时才需要重定向。
  • 我的错,(?!position)[^=]+=
  • 你很好用。我有这条规则RewriteRule ^.*-jobs/(.*)$ /jobs/$1 [R=301,NC,L],它重定向来自与-jobs 匹配的目录的所有请求。示例:/programming-jobs/ 等被重定向到 /jobs/。现在我的问题是如何使用没有目录的查询字符串重定向所有请求。换句话说,就是在站点根目录中的/ 之后附加?。在.com/ 之后,但只有在.com/? 之后附加查询字符串时才会重定向
猜你喜欢
  • 2020-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-03-24
  • 2018-10-10
  • 1970-01-01
  • 2021-04-05
相关资源
最近更新 更多