.htaccess 301 redirect to the same page with no qu

2019-09-07 00:21发布

I want to create a condition that if page has parameter in URL like ?print=1 - redirect this page to itself without any querystring.

Example:

I want this page:

http://sos-med.com/en/specific_page.html?print=1&ok=1 

tp redirect (301) to the same URL with no Query string:

http://sos-med.com/en/specific_page.html

My code is:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^*print=*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?

I have no test server, so can you tell me if my code is ok?

The code above is for every page on website. And before implementing that rule I would like to try the redirect for one specific page (see my example).
How to modify the code to work with "specific_page.html" only?

I want only .htaccess solution, not PHP code.

2条回答
乱世女痞
2楼-- · 2019-09-07 00:53

You're close, your %{QUERY_STRING} regex isn't right, and you're missing the 301 redirect flag:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}? [L,R=301]

Try that.


Thanks, and If I want to redirect single specific page: sos-med.com/en/aaa.html?print=1&ok=1 to sos-med.com/en/aaa.html ? –

Then you'd change what the rule matches against:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^print=.*$ [NC]
RewriteRule ^en/aaa.html$ %{REQUEST_URI}? [L,R=301]
查看更多
一夜七次
3楼-- · 2019-09-07 01:05

Try this one instead :

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?print= [NC]
RewriteRule ^(.*)$ /$1? [L,R=301]
查看更多
登录 后发表回答