【问题标题】:changing url 301 redirection with htaccess or php使用 htaccess 或 php 更改 url 301 重定向
【发布时间】:2012-11-20 18:06:24
【问题描述】:

example.com/a=process&b=5&b_id=5

如何将其重定向到

example.com/a=process&b_id=5 

所以我可以删除所有传入 url 的 b=5。但是 "a" 、 "b" 和 "b_id" 可变的不是静态的。我该怎么做?

【问题讨论】:

  • 您想重定向到另一个域?重定向的条件是什么-b和b_id的值应该相同?
  • 不,不是另一个域。我想用htaccess删除一个查询变量,url中有不同的变量,但我只想删除b变量
  • 您为什么要这样做?如果您不想使用 b,请不要在脚本中使用 b。
  • .htaccess 将在服务器端工作,并且浏览器的 url 将保持不变。所以我认为你真的需要解释更多的东西,这些 url 来自哪里,你希望什么时候发生重写,你为什么要谈论重定向等等。
  • 网址来自谷歌索引。我想要它用于谷歌

标签: php .htaccess url redirect http-status-code-301


【解决方案1】:

如果要进行内部重定向,请使用以下代码:

RewriteEngine On

# The query variable b is comming first.
RewriteCond %{QUERY_STRING} ^b=[0-9]*&(.*)$
RewriteRule ^(.*)$ %2?%1 [L,NS]

# The query variable b is comming in middle.
RewriteCond %{QUERY_STRING} ^(.*)&b=[0-9]*&(.*)$
RewriteRule ^(.*)$ %3?%1&%2 [L,NS]

# The query variable b is comming last.
RewriteCond %{QUERY_STRING} ^(.*)&b=[0-9]*$
RewriteRule ^(.*)$ %2?%1 [L,NS]

然后只打印print_r($_GET); 查询变量b 不会在数组中。

如果要进行外部重定向,请使用以下代码:

RewriteEngine On

# The query variable b is comming first.
RewriteCond %{QUERY_STRING} ^b=[0-9]*&(.*)$
RewriteRule ^.*$ %{REQUEST_URI}?%1 [R,L,NS]

# The query variable b is comming in middle.
RewriteCond %{QUERY_STRING} ^(.*)&b=[0-9]*&(.*)$
RewriteRule ^.*$ %{REQUEST_URI}?%1&%2 [R,L,NS]

# The query variable b is comming last.
RewriteCond %{QUERY_STRING} ^(.*)&b=[0-9]*$
RewriteRule ^.*$ %{REQUEST_URI}?%1 [R,L,NS]

查看http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html 了解 R、L、NS 等。

【讨论】:

  • 非常感谢。我不知道b从哪里来。我必须把上面的所有代码都写出来吗?我想要它给google。因为谷歌索引了我的网站链接,但我不想要 b 变量,因为不再使用它。
猜你喜欢
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-23
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 2012-10-19
相关资源
最近更新 更多