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

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.

Related

Can't pass parameters to another redirect website

I want to redirect to another website, and pass the parameters also.
Example: I go to my website: source.example/?code=12345
Then, I want it to redirect to target.example/?code=12345.
I am currently using this for my .htaccess file, since I figured out from other posts that if I query a certain parameter, it will get passed also:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^code=[NS]$
RewriteRule "www.google.com" /$1 [R=302,L]
Also, I tried many different approaches looking at these stack questions:
simple .htaccess redirect : how to redirect with parameters?
Redirect and keep the parameter in the url on .htaccess
But I can't get it running :(
since I figured out from other posts that if I query a certain parameter, it will get passed also
This is not true. The query string is passed through by default - there is nothing extra you need to do if you want the same query string on the target URL.
RewriteCond %{QUERY_STRING} ^code=[NS]$
RewriteRule "www.google.com" /$1 [R=302,L]
This code won't match the source URL for many reasons:
"www.google.com" - The first argument to the RewriteRule directive is a regex that matches the source URL-path (less the slash prefix). In your example the URL-path is empty.
^code=[NS]$ matches either code=N or code=S - which is not the intention from your example. (The [NS] looks like a mangled RewriteRule flag?!)
/$1 - this is the substitition string, ie. the URL you want to redirect to. (The $1 backreference is always empty, so this is meaningless.)
To redirect from source.example/?code=<number> to https://target.example/?code=<number> then try the following instead:
RewriteCond %{HTTP_HOST} ^source\.example [NC]
RewriteCond %{QUERY_STRING} ^code=\d+$
RewriteRule ^$ https://target.example/ [R=302,L]
This only matches a query string of the form code=1234. It does not match code= or code=1234&foo=bar, etc.
The query string is passed through by default.
If source.example is the only domain being hosted at the current location then you can remove the first condition that explicitly checks the requested hostname.
The order of directives in the .htaccess file is important. An external redirect like this should go near the top.

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.

.htaccess - silently rewrite/redirect everything to internal subfolder

Let's say I have thiswww.example.com site structure:
/srv/http/
/srv/http/site/index.php
/srv/http/site/stuff.php
I want the following rewrites/redirects to happen:
www.example.com/index.php -> redirects to -> www.example.com/site/index.php -> but the user sees -> www.example.com/index.php
www.example.com/stuff.php -> redirects to -> www.example.com/site/stuff.php -> but the user sees -> www.example.com/stuff.php
In general, everything after www.example.com/ redirects to www.example.com/site/. But the user sees the original URL in the browser.
I've looked around on the internet but haven't managed to figure out what to use in this particular situation.
I tried rewriting everything:
RewriteEngine On
RewriteRule ^$ /site [L]
but index.php disappears and www.example.com/site/ is shown to the user.
How can I use .htaccess to solve this problem?
You need to capture the url request incoming into the server, like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L,QSA]
The QSA is (eventually) to also append the query string to the rewritten url
Same idea as #guido suggested, but a bit shortened using negative lookahead
RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1 [L]
Note: I am not using QSA flag as we are not adding additional parameters to the query string for the replacement URL. By default, Apache will pass the original query string along with the replacement URL.
http://www.example.com/index.php?one=1&two=2
will be internally rewritten as
http://www.example.com/site/index.php?one=1&two=2
If you really want add a special parameter (ex: mode=rewrite) in the query string for every rewrite, then you can use the QSA Query String Append flag
RewriteEngine On
RewriteRule ^(?!site/)(.*)$ site/$1?mode=rewrite [L,QSA]
Then this will combine mode=rewrite with original query string
http://www.example.com/index.php?one=1&two=2
to
http://www.example.com/site/index.php?mode=rewrite&one=1&two=2
RewriteEngine On
RewriteRule ^(.*)$ site/index.php?var=$1 [L]
With this rule, i'm passing all requests to site/index.php, so you could get the requested uri via $_GET['var'], and then you'll make the index.php serve the requested url behind the scene without the url changing in the user's browser. Ciao.

What RewriteRule would be to redirect based on the on query string parameters?

If requested page page1.html and in query string uin is anything but not 12 or 13, let them see this page1.html page, otherwise redirect them to page2.html
Update: BTW, there are also other params in the query string. They should be sent to either page too.
The Rewrite Condition you're looking for is %{QUERY_STRING}
Here's another SO question doing something similar: Redirecting URLs (with specific GET parameters)
This will redirect to page2.html if uin=12 or uin=13. The entire query string will be sent to the page2.html page:
# EDIT: Doesn't properly handle all cases
RewriteCond %{QUERY_STRING} [\&]+uin=1[23][&]+ [OR]
RewriteCond %{QUERY_STRING} ^uin=1[23][&]+
RewriteRule ^/page1\.html /page2.html [R]
EDIT: This is a lot better and will handle the parameter in any position in the query string, beginning or end, and will also account for filtering out cases where the string is within another parameter, like suin=123
RewriteCond %{QUERY_STRING} ^(.*&)*uin=1[23](&.*)*$
RewriteRule ^/page1\.html /page2.html [R]
I tested on the following cases:
Redirected:
http://local.sandbox.com/page1.html?hello=world&uin=13&test=1
http://local.sandbox.com/page1.html?uin=12&test=1
http://local.sandbox.com/page1.html?uin=12
http://local.sandbox.com/page1.html?uin=13
http://local.sandbox.com/page1.html?uin=13&t=t
http://local.sandbox.com/page1.html?t=t&r=r&uin=13&t=3
http://local.sandbox.com/page1.html?t=t&uin=13
Didn't redirect:
http://local.sandbox.com/page1.html?uin=11&test=1
http://local.sandbox.com/page1.html?hello=world&uin=1&test=1
http://local.sandbox.com/page1.html?hello=world&ui=13&test=1
http://local.sandbox.com/page1.html?t=t&&r=r&suin=13&t=3
http://local.sandbox.com/page1.html?t=t&&r=r&uin=134&t=3
http://local.sandbox.com/page1.html?suin=134&t=3
http://local.sandbox.com/page1.html?auin=13&t=t
http://local.sandbox.com/page1.html?uin=134&t=3
http://local.sandbox.com/page1.html?t=t&uin=134
http://local.sandbox.com/page1.html?t=t&auin=13