简体   繁体   中英

htaccess - how to redirect url with params to base url

I am trying to redirect a page.html with params at the end to the base url without params. I know I am supposed to use mod rewrite but I can't get the rules figured out. Your help will be appreciated.

Here is the scenario: I tried this in htaccess and this rule didn't work:

redirect 301 "/page.html?p=2" http://www.domain.com/page.html

I understand from reading other posts on stackoverflow that I should write something like

^page\.html$ http://www.domain.com/page.html [QSA,NC,R=301,L]

The rules at the end [QSA,NC,R=301,L] however are incorrect. I can't figure it out. I simply want to do a 301 for this url with the p=2 parameter, and no other param.

And then process the remaining rules after this rule for other pages.

You can put this code in your root htaccess

RewriteEngine On

RewriteCond %{QUERY_STRING} ^p=2$ [NC]
RewriteRule ^page\.html$ /page.html? [R=301,L]

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