htaccess - add string to end url

2019-08-15 06:58发布

问题:

The url is like

http://www.example.com/?p=3733&preview=true

and I want to add ?ModPagespeed=noscript

so it becomes http://www.example.com/?p=3733&preview=true?ModPagespeed=off

p= is dynamic btw

Any ideas?

回答1:

put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{QUERY_STRING} (^|&)p=[^&]+
RewriteCond %{QUERY_STRING} (^|&)preview=true(&|$)
RewriteCond %{QUERY_STRING} !(^|&)ModPagespeed=off(&|$)
RewriteRule ^$ %{REQUEST_URI}?ModPagespeed=off [L,QSA,R]

UPDATE: If you want final URL with 2 ? in it, use this rule:

RewriteCond %{QUERY_STRING} (^|&)p=[^&]+
RewriteCond %{QUERY_STRING} (^|&)preview=true(&|$)
RewriteCond %{QUERY_STRING} !(^|&)ModPagespeed=off(&|$)
RewriteRule ^$ %{REQUEST_URI}?%{QUERY_STRING}?ModPagespeed=off [L,NE,R]