htaccess redirect with query string not working - .htaccess

enter code hereI have a WordPress website.
I have URLs for affiliates that look like this:
https://example.com/folder/?ref=23432
https://example.com/folder/?ref=13442
etc.
I would like to redirect any URL that ends in ?ref= to another domain.
For example, https://example.com/folder/?ref= should redirect to https://example.org/product/
How can I do this? I appreciate your time.
I tried
Redirect 301 example.com/folder/?ref https://example.org/product/
Thank you #MrWhite. I tried the following with no success.
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]

RewriteRule ^example.com https://www.example.org/product/$0 [R=302,L]
The RewriteRule directive matches the URL-path only (less the slash prefix). So this should be matching against folder/ (as per your example), not the hostname.
And the $0 backreference in the substitution string is not required here. So this should simply be:
:
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
If you do need to check the requested hostname (ie. example.com) - if example.com and example.org point to the same server - then you need a separate condition (RewriteCond directive). For example, the complete rule would then become:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} (^|&)ref=
RewriteRule ^folder/$ https://www.example.org/product/ [R=302,L]
Note that the regex (^|&)ref= matches the ref= URL parameter anywhere in the query string, if there happened to be other URL parameters that preceded it.
Reference:
htaccess redirect URL with parameter when a special parameter exists
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

Related

How can I redirect subdomain.domain.com/* to domain.com/folder/*

I have to make a redirection on a .htaccess file.
I want to access the content of a folder from a subdomain url like this:
subdomain.domain.com/* => domain.com/folder/*
The folder contains pdf files and I want to acces it with this url for example:
subdomain.domain.com/file.pdf
I'm new into htaccess redirection rules and I'm a little lost.
I tried something like this and test it into https://htaccess.madewithlove.com/
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
This code works on the tester but on my website it throws me the error : "The connection was reset".
Do you have any idea on it?
UPDATE
Following some advices I try but it doesn't work
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com/
RewriteRule (.+\.pdf)$ https://example.com/folder/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^subdomain.domain.com/*$
RewriteRule ^(.*) https://domain/folder/ [L,R=301]
You are not doing anything with the captured URL-path (ie. (.*)) so this will always redirect to https://domain/folder/ (no file).
I would also question whether this should be a 301 (permanent) redirect. Maybe a 302 (temporary) redirect would be preferable here? Note that the 301 is cached, so you will need to clear your browser cache before testing.
It should be like this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com
RewriteRule (.*) https://domain/folder/$1 [R=302,L]
The $1 backreference contains the URL-path captured in the RewriteRule pattern.
The Host header (ie. the value of the HTTP_HOST server variable) contains the hostname only. There is no URL-path component. (Fortunately /* matches the slash 0 or more times, so it still "worked" in your testing.)
However, if the folder only contains .pdf files then you should be more restrictive and redirect only .pdf requests. For example:
:
RewriteRule (.+\.pdf)$ https://domain/folder/$1 [R=302,L]

Remove trailing dot in URL domain

We have URLs of the form:
www.dev-studio.co.uk.
www.dev-studio.co.uk./a-sample-image
With the help of .htaccess rules, I am trying to remove the trailing dot (co.uk.) in the end of the domain name but I'm failing.
This is the rule I'm trying:
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.dev-studio.co.uk/%3 [L,R=302,NE]
But the %3 which should capture the 3rd group is returning empty.
The goal is to simple redirect www.dev-studio.co.uk./a-sample-image to www.dev-studio.co.uk/a-sample-image
I have tried all the other questions over here but the solutions are not working for me.
Any help would be appreciated.
RewriteCond %{HTTP_HOST} ^([a-z0-9\.-]+)(\.co\.uk\.)(.*)$
RewriteRule ^ http://www.example.co.uk/%3 [L,R=302,NE]
The HTTP_HOST server variable contains the hostname only (ie. the value of the Host HTTP request header), it does not contain the URL-path, so the %3 backreference is always empty.
You need to either capture the URL-path from the RewriteRule pattern. For example:
RewriteRule (.*) http://www.example.co.uk/$1 [R=302,L]
Or, use the REQUEST_URI server variable (which contains the full URL-path, including slash prefix) instead:
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
This should ultimately be a 301 (permanent) redirect, once you have confirmed it works OK.
Note that since you are redirecting to a specific domain, do you need a CondPattern that matches any .co.uk hostname? You could be specific:
RewriteCond %{HTTP_HOST} =www.example.co.uk.
RewriteRule ^ http://www.example.co.uk%{REQUEST_URI} [R=302,L]
The = prefix on the CondPattern changes it to a lexicographical string comparison (not a regex), so no need to escape the dots.
If you wanted an entirely generic solution to remove the trailing . (FQDN) from any requested host then you could do something like:
RewriteCond %{HTTP_HOST} (.+)\.$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]
Although you might want to combine this with your canonical redirects (eg. non-www to www / HTTP to HTTPS?) to avoid multiple redirects - although they are probably unlikely to occur all at once anyway, so probably not an issue.

301 Redirects with mod_rewrite

I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :
http://domain/index.php?folder=AB_CD
to an URL like this
http://domain/AB/CD/
How can I write the rule please ?
Try the following code in root/.htaccess :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
Explaination :
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
index.php?query_strings gets redirected to /query/strings, if the condition is met.
Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.
(Hope, this helps!)

301 Redirects problem - urls with ? and =

I'm new with 301 redirects through .htacces.
I can get simple redirects as
redirect 301 /test.html http://www.domain.com/test2.html
to work but I have some urls like this
redirect 301 /test.asp?Group=100 http://www.domain.com/test3.html
and for some reason these don't work.
Thanks.
Here is set of rules for URLs you have provided:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =group=113 [NC]
RewriteRule ^group\.asp$ http://domain.dk/til-born.htm? [NC,R=301,L]
RewriteCond %{QUERY_STRING} =product=1136 [NC]
RewriteRule ^product\.asp$ http://www.domain.dk/til-born/bukser.html? [NC,R=301,L]
As you can see query string is matched separately to the page name. So .. for each of such redirects you need 2 lines: RewriteCond & RewriteRule.
The rule above will do EXACT match, which means /group.asp?group=113&param=value will not be redirected because query string is group=113&param=value which is more than just group=113.
To have such redirect working (when there are some optional parameters in query string) you have to modify it: RewriteCond %{QUERY_STRING} (^|&)group=113(&|$) [NC] -- this will match group=133 anywhere in query string (group=113 and group=11366 are still different, so no problems here).
This needs to be placed in .htaccess in website root folder. If placed elsewhere some tweaking may be required.
The Redirect directive (as far as I know) matches only on the path, not querystring. Instead, use RewriteRule. The QSA instructs the rewrite engine to append the querystring onto the new redirected URL.
RewriteEngine On
RewriteRule ^test\.asp http://www.domain.com/test3.html [L,R=301,QSA]

.htaccess redirect from GET variable to url string

I need to redirect
/search?keywords=somesearchterm
to
/search/somesearchterm
This seems incredibly basic but I've been pounding my head against it for an hour.
Thanks for taking the time to look at this.
You want to implement what is called a "301 Redirect" with mod_rewrite.
RewriteEngine ON
RewriteRule ^/search\?keywords=somesearchterm$ /search/somesearchterm
adding regular expressions:
RewriteEngine ON
RewriteRule ^/search\?keywords=(.+) /search/$1 [R=301,L]
R=301 means provide a 301 Header redirect so the user's URL changes in the browser, and L means don't process any more rewrite rules if this one matches.
If you want to do the reverse -- in other words, if someone goes to mysite.com/search/asearchterm and you want the URL to stay the same, but "behind the scenes" you want it to load a certain server script, do this:
RewriteEngine ON
RewriteRule ^/search/(.+) /search.php\?keywords=$1 [L]
You can not match aginst Query string in RewriteRule directive. Use %{THE_REQUEST} or %{QUERY_STRING} server variables to match the Query string :
The following rule works fine for this redirection
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/search\?kewords=([^&\s]+) [NC]
RewriteRule ^ /search/%1? [NE,NC,R,L]
RewriteRule ^search/([^/]+)/?$ /search?keyword=$1 [QSA,NC,L]

Resources