簡體   English   中英

使用htaccess或php更改URL 301重定向

[英]changing url 301 redirection with htaccess or php

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

我該如何重定向到

example.com/a=process&b_id=5 

因此,我可以刪除所有傳入網址的b = 5。 但是“ a”,“ b”和“ b_id”可變不是靜態的。 我怎樣才能做到這一點?

如果要進行內部重定向,請使用以下代碼:

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等。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM