Remove query string from url using rewrite rule - apache

Im trying to rewrite url in htaccess file.
Original url:
www.mywebsite.com/flash/module.swf?xmlpath=http%3a%2f%2f0.0.0.0%3a8000%2fconfig.xml
I want to remove query string from this url. This condition should applied only for .xml extension.
Output URL should be
www.mywebsite.com/flash/module.swf

You can match a query string with a RewriteCond and %{QUERY_STRING}. If the second argument of a RewriteRule contains a query string, it will overwrite the existing query string. Clearing the query string is as easy as appending a ? to your url ;-) Alternativelly, you can use the QSD (Query String Discard) flag.
RewriteCond %{QUERY_STRING} \.xml$
RewriteRule ^flash/module\.swf$ flash/module.swf? [L]
Change the flags ([L]) to [R,L] or [R=301,L] if you want this to be an external redirect instead.

Related

Rewrite URL containing directory path and parameters to parameter based URL using both path and query string

I use htaccess to rewrite this path:
/inventory/products/tools/
to this url with query string:
/inventory.php?cat=products&type=tools
using the following rule:
RewriteRule ^inventory/(.*)/(.*)/? /inventory.php?cat=$1&type=$2 [L,R=301]
When I add a query string to my url path
/inventory/products/tools/?sort=pricehigh
and use this rule
RewriteCond %{QUERY_STRING} ^(.*)$ [NC]
RewriteRule ^inventory/(.*)/(.*)/? /inventory.php?cat=$1&type=$2&%1 [L,R=301]
I am getting a redirect loop and the urlstring is rewritten over and over
I am trying to end up with the following destination url
/inventory.php?cat=products&type=tools&sort=pricehigh
In the example rule above I am using R=301 in order to visualize the url.
In a production I would use [L] only
Without the trailing slash, the second (.*) also allows for matching zero characters - so due to the greediness of regular expressions, the first (.*) matches products/tools already.
The following should work:
RewriteRule ^inventory/([^/]+)/([^/]+)/?$ /inventory.php?cat=$1&type=$2 [QSA,L,R=302,NE]
([^/]+) demands one or more characters, out of the class of characters that contains everything but the /.
The NE/noescape flag seems necessary here for some reason, otherwise the resulting query string will contain ?cat=products%26type=..., with the & URL-encoded.

RewriteRule expression not working

I want to rewrite a URl to another, but the Rewrite expression is not working.
Ex. URL1: "http://www.demo.com/de-ch/case1/?id=23"
And
Ex. URL2: 'http://www.demo.com/de-ch?id=23'
RULE: RewriteRule ^(.....)([\?]|(.*))$ http://www.result.com/$1
This rule gives follow result:
http://www.result.com/de-ch?id=23
But I just need the iso codes http://www.result.com/de-ch
What am I missing in the Regex?
You can't match against the query string in the rule's pattern. The query string isn't technically part of the path. You'll need to use a RewriteCond and the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^id=23
RewriteRule ^(.....) http://www.result.com/$1?
Note the "?" at the end of the rule's result. That will remove the query string.
However, since you're not doing anything with the query string, you can just leave it out entirely and only have:
RewriteRule ^(.....) http://www.result.com/$1?
Note that this rule will cause the browser to get redirected to http://www.result.com/de-ch and if you don't have a resource that resolves at that URL, then you'll simply get a 404 or some related error.

Remove Query String from the end of the URL after passing through Rewrite Map

I have a list of several hundred URL redirects/rewrites inside of a rewrite map. Most of the URL's contain query strings that match a specific entry in the rewrite map. I found this question on how to pass the query string into the rewrite map. I got this working fine but the problem now is that the existing query string is appended to the end of the rewritten URL. For example:
Expected Rewrite:
/subdir/dir.cfm?categoryID=123 -> https://example.com/subdir/endurl
Actual Rewrite:
https://example.com/subdir/endurl?categoryID=123
Expected Rewrite
/subdir/dir.cfm?videoID=3422424-131FDDFD-234 -> https://example.com/subdir/awesome/stuff
Actual Rewrite:
https://example.com/subdir/awesome/stuff?videoID=3422424-131FDDFD-234
This is the rewrite rules I have:
RewriteEngine on
RewriteMap redirects dbm=sdbm:C:\Apache24\conf\redirects.sdbm
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}} [NC,R=301,L]
How can I remove the query string appended to the URL after the rewrite?
EDIT
I was able to actually get this working using this:
RewriteMap redirects dbm=sdbm:C:\Apache24\conf\redirects.sdbm
RewriteCond ${redirects:$1} !=""
RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}} [NC,C]
RewriteRule (.*) $1? [L,R=301]
However I am not sure if someone knows of a better way to accomplish what I am doing? I am thinking this might break if I ever have to redirect to a URL that contains a query string to another url that contains a query string.
In Apache version 2.4.0 and later, you can use [QSD] flag (Query String Discard):
RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}} [QSD,NC]
In Apache version 2.2, there is no [QSD] flag, but if the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, so by just adding a ? to your rewrite URI, you will get the same effect:
RewriteRule ^(.*\.(cfm|cfml)) ${redirects:$1?%{QUERY_STRING}}? [NC]
(your newly added rewrite rule actually uses this feature)

apache query string rewrite rules

I am setting up Query string redirect :
expo.com/en/general/campaigns/on-second-thought.html?slide=ost-2016-tank to
expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} slide=ost-2016-tank
RewriteRule  ^/en/general/campaigns/on-second-thought.html?$  http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC]
redirect happening but its appending ?slide=ost-2016-tank like below
http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html?slide=ost-2016-tank
slide=ost-2016-tank parameter is added to redirected page
Since your rule does not define a new query string, the default behavior of Apache is to copy the old query string to the new URL. To get rid of it, append a ? to the address you rewrite/redirect to:
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html? [R=301,L,NC]
Or, for Apache >= 2.4, you can also use the flag QSD (Query String Discard):
RewriteRule ^/en/general/campaigns/on-second-thought\.html?$ http://www.expo.com/en/general/campaigns/on-second-thought/ost-2016-tank.html [R=301,L,NC,QSD]
Simply add a blank query string when redirecting:
RewriteCond %{HTTP_HOST} ^(.*)expo\.com
RewriteCond %{QUERY_STRING} ^slide=(ost-2016-tank)$
RewriteRule ^(/?en/general/campaigns/on-second-thought)\.(html)$ $1/%1.$2? [R=301,L,NC]
No need to mention http://expo.com again when redirecting. It'll automatically redirect to the same hostname because of R flag. No need to repeat same strings over and over. Using match groups and referencing them later works.
Your pattern had .html?$ in it, which actually means that it'll match .html as well as .htm. You do not receive query strings in RewriteRule context.

Rewrite rule to redirect a URI without a query param after appending a query param

I need to check if a UIRI from a folder contains any query param and if not, add the query param and redirect the uri in browser also using Apache rewrite rule. For this all url's starting with /abc/def/xyz/ the url should be appended with ?v=2, if not already having a query param
For example, /abc/def/xyz/folder/test.pdf should become /abc/def/xyz/folder/test.pdf?v=2
But /abc/def/xyz/folder1/test.pdf?v=3 should be left untouched.
I was able to add the query param using below but it causes infinite redirects. Thats why I need the selective redirect
RewriteRule ^abc/def/xyz/(.*) /abc/def/xyz/$1?v=2 [L,R=301]
the below does not redirect the browser URL to the new URI with the query param:
RewriteRule ^abc/def/xyz/(.*) /abc/def/xyz/$1?v=2 [PT,L]
You can make use of RewriteCond to check for Query Strings. I guess the following should do the trick. If there is a query String available, it would skip the rule. Else, it would append the query string v=2
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^abc/def/xyz/(.*)$ /abc/def/xyz/$1?v=2 [L, R=301]
Hope it helps.