简体   繁体   中英

htaccess url rewrite with RegEx

I want to redirect some url, but got some problem with it. The urls like:

/sample?back=my-account
/sample?back=history
/sample?back=addresses

And need to redirect to

/sample

My last try was this (but didn't work):

RewriteCond %{QUERY_STRING} ^?back=history$
RewriteRule ^sample$ /sample [R=301,L]

You can use this fix:

RewriteCond %{QUERY_STRING} ^back=
RewriteRule ^sample$ /sample? [R=301,L]

This will redirect http://example.com/sample?back=my-account to http://example.com/sample .

Apparently, your condition was not met since the query string starts with back , and to get rid of the query string, you need to add ? to the end of replacement string.

Apache Module mod_rewrite documentation :

When you want to erase an existing query string, end the substitution string with just a question mark.

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