Strip specific parameteters when redirecting with Mod-Rewrite - apache

I have a pretty complex RewriteRule where I need to check if certain parameters are present in QueryString and then redirect to the same URL but with those parameters stripped.
How can I remove some parameters and preserve the rest?
RewriteCond %{QUERY_STRING} color=red
RewriteCond %{QUERY_STRING} status=contiue
RewriteRule ^(.*)$ /$1? [R=301,L]
url is like:
"http://example.com/site.php?setup=done&color=red&weight=100&status=continue"
(parameters order and quantity is not predictable/hardcoded)

Try these rules:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)(color=red|status=continue)($|&)(.*)
RewriteRule .* $0?%1%5 [N,E=REMOVED:true]
RewriteCond %{ENV:REMOVED} true
RewriteRule ^ %{REQUEST_URI} [L,R=301]
Another way would be to use PHP to check what parameters are given and remove them there.

Related

htaccess redirect using get parameters and remove same parameters

Please help me need redirect search.html?searchword=value&searchphrase=all to search.html?searchword=value
I tried:
RewriteCond %{QUERY_STRING} ^searchword=(.*) [NC]
RewriteCond %{QUERY_STRING} searchphrase= [NC]
RewriteRule ^(.*)$ /search.html\?searchword=%1 [R=301,L]
but it do not work.
You may use it like this:
RewriteCond %{QUERY_STRING} (?:^|&)searchphrase= [NC]
RewriteCond %{QUERY_STRING} (?:^|&)(searchword=[^&]*) [NC]
RewriteRule ^search\.html$ %{REQUEST_URI}?%1 [R=301,L,NC,NE]
Changes are:
Order of RewriteCond is important. Keep the condition with capture group as last one
No need to repeat searchword again in target, just capture from RewriteCond and use it later as back-reference %1
Instead of using .* use [^&]* to match only value till you get next & or end of string
Match search.html in rule pattern to avoid matching anything else
With your shown samples and attempts please try following .htaccess rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(search\.html)\?(searchword=[^&]*)&searchphrase=all [NC]
RewriteRule ^ /%1?%2 [R=301,L,NE]

Apache manipulating query string on https

the following rules works on http but don't on https.
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1? [L]
RewriteRule ^/path/file.html$ https://www.domain.tld/path/file/ [R=301,L]
Why the query_string part doesn't work in https?
Based on the comments of your question, it appears that you just want to remove the query string anytime there is a page parameter. This type of rewrite rule doesn't strip off or change the URL unless there is a redirect. So if you don't add R=301 or R to the rewrite rule, the query string will not be removed. All of the following worked on my server to remove the query string, and my server is 100% HTTPS:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1? [R=301,L]
Or you may be able to use the QSD flag instead of question mark:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1 [R=301,L,QSD]
Or you may be able to use something like this:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule .* /? [R=301,L]
Or just R (for 302) instead of R=301:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1? [R,L]
But in no case was the query string removed unless I used a redirect.
Really all you need is QSD http://httpd.apache.org/docs/current/rewrite/flags.htm
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1 [L,QSD]
I'm not sure why it would work on http but not https unless you're using separate vhosts for http and https and the settigns were slightly different
Remember that Rewrite rules are internal unless using the R flag. When you use the R flag it tells the browser to go to a different page causing a full server/client round-trip. Otherwise, it just changes the request and proceeds as normal.
I'm not really strong with .htaccess file, and I can't explain what's going on behind the scenes, but from my opinion your .htaccess file should look like this:
RewriteCond %{QUERY_STRING} page= [NC]
RewriteRule (.*) /$1? [L]
RewriteRule ^path/file.html$ https://www.domain.tld/path/file/ [R=301,L]
It work perfectly for
http://example.com?page=1
https://example.com/?page=1
http://example.com/path/file.html
https://example.com/path/file.html
Tested (with love) here

Create RewriteRule Split query string in .htaccess for _escaped_fragment_

I am redirecting requests to snapshots directory for google hashbang.
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
RewriteRule ^(.*)$ /snapshots/%1? [NC,L]
So: http://site/?_escaped_fragment_=/detail/10
Go here: site/snapshots/detail/10
But I want to ignore the second params so
http://site/?_escaped_fragment_=/detail/10/12-12-2014
Should be redirect to the same url
site/snapshots/detail/10
Any idea how I should rewrite the rules?
Try changing this line:
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?(.*)$
to
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=/?([^/]+(?:/[^/]+|))
So the regex will only attempt to capture as many as 2 virtual paths.

Create a dynamic RewriteRule

Is is possible to create a RewriteRule that conatins a dynamic parameter (partial url)?
Here is the existing Rewrite:
RewriteCond %{SERVER_NAME} ^dashdiscovery-dev.site.com
RewriteRule ^/?auth/?$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=/views&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
What I need to to get the full address that is requested and put it in the Rewrite appURI= parameter dynamically.
Something like this:
RewriteCond %{SERVER_NAME} ^dashdiscovery-dev.site.com(/view/page7)
RewriteRule ^/?auth/?$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=(**/view/page7**)&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
But I also need it to redirect to just /views is there isn't any additional parameter after the site name RewriteCond %{SERVER_NAME} ^dashdiscovery-dev.site.com
Edit to provide more info about the expected urls:
dashdiscovery-dev.site.com
would need to have appURI=/views in the Rewrite
dashdiscovery-dev.site.com/views/ResourceManagerDashboardv10-3-15-13_bkup/4DemandvsBooking
would need to have appURI=/views/ResourceManagerDashboardv10-3-15-13_bkup/4DemandvsBooking in the Rewrite
dashdiscovery.site.com/views/OpsPipeline/PipelineDash
would need to have appURI=/views/OpsPipeline/PipelineDash in the Rewrite
not all URLs will be only 2 levels past /views, but they should all have /views
For dashdiscovery-dev.site.com:
RewriteCond %{HTTP_HOST} ^dashdiscovery-dev\.site\.com$ [NC]
RewriteRule ^/?$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=/views&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
For everything else:
RewriteCond %{HTTP_HOST} ^dashdiscovery-dev\.site\.com$ [NC]
RewriteRule ^/?(views/.*)$ https://osso-stg.site.com/opensso/idpssoinit?realm=/sitenet&iPSPCookie=yes&RelayState=app=tableau,appURI=/$1&NameIDFormat=urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress&metaAlias=/sitenet/externalidpv2&spEntityID=server.site.com&binding=urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST [L,R]
The $1 in the rule's target backreferences the (views/.*) regex match.

RewriteRule to disregard a url variable

I have some pages indexed by Google, for example:
/product.html?affiliateid=142
I want a rewrite rule to 301 redirect to the same page if there's an affiliateid=xxx
So far I have this:
RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^$ /test.html$ [L,R=301]
But its not working, I need to get rid of the variable and get the page name somehow.
You need to specify an empty query in your substitution URL to have the original requested query not appended to the new URL:
RewriteCond %{QUERY_STRING} ^affiliateid=[0-9]+$
RewriteRule ^ %{REQUEST_URI}? [L,R=301]
And if you want to preserve any other query parameter, try this:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)affiliateid=[0-9]+(&+(.*))?$
RewriteRule ^ %{REQUEST_URI}?%1%4 [L,R=301]
Found it:
RewriteCond %{QUERY_STRING} ^affiliateid=([0-9]+)$
RewriteRule ^(.*)$ /$1? [L,NC,R=301]
It's that question mark here /$1? that tells the rule to end the rewrite at the query string