问题
I need to redirect all url's that contain the parameter ?lang= to my base url.
For example: If www.example.com/?lang=ru redirect to www.example.com.
I have tried with RedirectMatch but it's not working.
Thanks!!!
回答1:
The Redirect* directives do not handle the query string so you need to use URL rewriting instead.
RewriteEngine on
# if the query string contains a parameter called "lang"
RewriteCond %{QUERY_STRING} (?:^|&)lang=
# then redirect (permanently) to /
RewriteRule ^ / [L,R=permanent]
回答2:
Using Regular Expressions
If you want to use a Regular Expression to redirect something, use the RedirectMatchdirective:
RedirectMatch "^/lang=ru?.html/?$" "http://example.com/newfile.php"
Don't forget have mod_rewrite on in your apache configuration, and other configuration neccessary for enable your htaccess file.
来源:https://stackoverflow.com/questions/52497336/htaccess-redirect-to-base-url-if-url-contains-parameter