简体   繁体   中英

changing url 301 redirection with htaccess or php

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

how can I redirect this to

example.com/a=process&b_id=5 

So I can delete b=5 for all incoming url. But "a" , "b" and "b_id" changable not static. How can I do this?

If you want to do internal redirection, use the following code:

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]

Then just print the print_r($_GET); The query variable b won't be in the array.

If you want to do external redirection, use the following code:

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]

Have a look at http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html to know about the R, L, NS, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM