htaccess redirect to base URL if URL contains parameter

耗尽温柔 提交于 2020-06-28 04:56:16

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!