Redirect rewrite rule, Adding parameter at the end of url - apache

If /c/ is part of URL parameter then I want to add parameter at the end of URL parameter. because parameter may increase or decrease.
http://example.com/c/file.php?par1=val1&par2=val2
I need add two parameter &addpar1=val&addpar2=val at the end of URL like this.
http://example.com/c/file.php?par1=val1&par2=val2&addpar1=val&addpar2=val
What I am trying to do here:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/c/(.*)$ [NC]
RewriteRule /c/ /%1 [QSA]
Please suggest me what should written in RewriteRule here.

Your rule is close, but you're not actually adding anything to the query string. Try:
RewriteEngine On
RewriteCond %{QUERY_STRING !&addpar1=val&addpar2=val
RewriteRule ^/?c/(.*)$ /%1?%{QUERY_STRING}&&addpar1=val&addpar2=val [L]
Here, you need to check that the parameters has already been added, then you add them to the end of the query string. You don't want the QSA flag here because you're manually doing the appending.
If you want to redirect the browser so that they see the query strings then you need an R or R=301 flag in the square brackets (separated by a comma).

Related

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.

htaccess rule to remove parameters

struggling to come up with a .htaccess rule to remove parameters from it.
Essentially I want
http://www.example.com/page/competition.html?utm_medium=email&utm_campaign=11&utm_source=11&utm_term=11.gif
to rewrite to
http://www.example.com/page/competition.html
so, anything right from the /page/compeition.html is rewritten.
Grateful for any assistance. thanks in advance
In your question, you state that you want the long URI rewritten to the shorter one. Unfortunately, that isn't very clear. Assuming that you wish to simply remove the query string using a redirect, you can use the following:
RewriteCond %{QUERY_STRING} ^utm_medium=email&utm_campaign=([^&]+)&utm_source=([^&]+)&utm_term=([^&]+)$
RewriteRule ^ %{REQUEST_URI}? [R=302,L]
This is a generalised condition and rule that will check for any utm information in the query string. If it is present, then strip out the query string.
If you wish to only do this for page/competition.html, then change the rule to this:
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
If you wish to simply remove any query string forming part of a request to page/competition.html, then you can use this instead of the above:
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(page/competition\.html)$ /$1? [R=302,L]
This basically tells Apache to check if the query string is not empty and, if it isn't, strip it out altogether.
To make the redirect permanent, change 302 to 301.
As a general rule, you should be able to use the following:
RewriteRule ^(.*)$ $1? [END,QSA]
The key in this rule is the trailing questionmark which will not appear in the output URL.
RewriteRule ^(.*)$ $1? [END,QSA] This rule was met, the new url is
http://www.example.com/page/competition.html
Try it out at the following:
http://htaccess.madewithlove.be/
Do you want that only for the specific page? Or more as a general approach?
RewriteRule ^page/competition.html?(.+)$ /page/competition.html [L,NC]
That would be the very easiest form to achieve it, but would only work for the exact filename. For a more general approach, you would need to add some more placeholders.

.htaccess Add a parameter to query string if using certain subdomain

I would like to add a fixed parameter to a URL if a certain sub domain is used, but otherwise leave the URL untouched.
For example:
http://subdomain.domain.com?foo=bar
would rewrite to
http://subdomain.domain.com?foo=bar&isMySub=true
but http://domain.com?foo=bar or http://othersubdomain.domain.com?foo=bar would remain untouched.
Ive looked around on here and so far all Ive been able to ascertain is that it will likely require the [QSA] flag so as to leave the rest of the query string intact.
This get me close, but some links and images break so it has to be messing up my url or query in ways I dont want.
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com(&|$)
RewriteRule .* index.php?foo=bar [QSA]
You can exclude requests for certain files using the %{REQUEST_URI} server variable and checking the extension:
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule .* index.php?isMySub=true [QSA]
The [QSA] flag will pass the existing query string, such as ?foo=bar, so you don't have to.
Alternative rules for the above ruleset:
Send to index.php and add requested resource as the query variable req:
RewriteRule ^(.*)$ index.php?req=$1&isMySub=true [QSA]
The parenthesis () in the rule pattern capture the matching value into a variable. First set of parens goes to $1, second to $2, etc.
Or, send to same resource as is requested but with the added query variable.
RewriteRule ^(.*)$ $1?isMySub=true [QSA]

mod_rewrite: encode only certain matches in URL

I am trying to rewrite a URL using mod_rewrite and encode a substring in my URL which is between brackets. My URL:
http://localhost/something?var_a=A&var_b=(B&2/3&)&var_c=C
and my .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} (.+)\((.*?)\)(.+)
RewriteRule ^.*$ somedir/%1%2%3? [R,B]
so I capture three strings, anything before the brackets, anything within, and anything after.
Result is:
somedir/var_a%3dA%26var_b%3dB%262%2f3%26%26var_c%3dC
but I would like to encode only the text which was within the brackets of my initial URL, such that
somedir/var_a=A&var_b=dB%262%2f3%26&var_c=C
The problem seems to be that the [B] option decodes the whole string. Is there a way to do this selectively? Also, my solution could only capture an occurrence of brackets once, it would be nice to have this more generic; could someone give me a hint?
Note that this question is related to my previous one, where I was trying to capture text between brackets.
This is extremely tricky for mod_rewrite but I took a shot at it. Solution is not pretty as it involves 2 redirects and use of cookies, but it works.
RewriteEngine On
# store non bracket query string in cookie while redirecting value in brackets using B flag
RewriteCond %{QUERY_STRING} ^(.+?&)?(var_b=)\(([^)]*)\)(.*)$ [NC]
RewriteRule !^somedir/ /somedir/%3? [L,CO=QS:%1-%2-%4:%{HTTP_HOST},B,R]
# retrieve value from cookie and use it to construct full URL
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_COOKIE} QS=([^-]*)-([^-]+)-([^;]+) [NC]
RewriteRule ^(somedir)/(.*)$ /$1/%1%2$2%3? [L,R,NE]
Using these rules when I visit this URL: http://localhost/something?var_a=A&var_b=(B&2/3&)&var_c=C
it gets redirected to: http://localhost/somedir/var_a=A&var_b=B%262%2f3%26&var_c=C

How to capture the parameter value by mod_rewrite

I'm using Apache2.2.17 and am trying to redirect the URL like below with the same parameter value being preserved but having a different key name.
/aaa/bbb/ccc?oldkey=value => /ddd?newkey=value
I'm trying some variations of RewriteRule like below but not successful.
RewriteRule /aaa/bbb/ccc?oldkey=(.*) /ddd?newkey=$1 [R,L]
Then I noticed that if I have the condition like below, only the last path value is preserved and parameter key/value are lost.
RewriteRule /aaa/bbb/(.*) /ddd?newkey=$1 [R,L] ;This produces /ddd?newkey=ccc
So my question is,
Is there a way to capture the parameter value by using Apache mod_rewrite and how can I achieve that? (I can use mod_proxy too)
Thank you in advance.
You can check what's in %{QUERY_STRING}:
RewriteCond %{QUERY_STRING} oldkey=(.*)
RewriteRule .* /ddd?newkey=%1 [R,L]
Where %1 is the first capture group from RewriteCond (called RewriteCond backreference).
If by "parameter value" you mean the querystring then you can append it to your rewrites by adding QSA to your command so you have [QSA,R,L]