Htacces redirecting on url with parameter - .htaccess

I want to redirect via htacces on url with parameter ofr example:
domain1.com/asd -> domain2.com/sdew;as
domain1.com/ase -> domain2.com/sdew;aw
I use in htacces conditions as bellow:
RewriteCond %{HTTP_HOST} ^domain1.com$ [NC]
RewriteRule ^aa/bb-l(/.*)?$ http://www.domain2.com/ssd/sdew;as [R=301,L,NC]
This rule redirect from domain1.com/asd to http://www.domain2.com/ssd/sdew%3bas
not to http://www.domain2.com/ssd/sdew;as

Change flags [R=301,L,NC] to [R=301,L,NC,NE]
NE|noescape
By default, special characters, such as & and ?, for example, will be
converted to their hexcode equivalent. Using the [NE] flag prevents
that from happening.
http://httpd.apache.org/docs/2.2/rewrite/flags.html

Related

htaccess redirect with query string not working

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

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.

add string at the end of URL using htaccess which has query string also

I want to change
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
That is if the URL contains index.php?members, then I add /#div at the end of url. I tried this
RewriteEngine On
RewriteCond %{REQUEST_URI} index.php?
RewriteRule (.*) /%1/%{QUERY_STRING}&123 [L,QSA,R=301]
but it returns
domain.com/members/maxmusterman.5&123?members/maxmusterman.5
Note here that &123 is attached after URI before starting parameters. I researched htaccess QSA flag but I could not find a way to add a custom string at the end of the query string. How can I do that. Here I have used &123 for test purpose, actual requirement is adding /#div
To redirect
domain.com/division1/index.php?members/maxmusterman.5
to
domain.com/division1/index.php?members/maxmusterman.5/#div
.
You can use something like the following :
RewriteEngine on
RewriteCond %{QUERY_STRING} !loop=no
RewriteRule ^division1/index\.php$ %{REQUEST_URI}?%{QUERY_STRING}&loop=no#div [L,R,NE]
I added an additional perameter loop=no to the destination url to prevent infinite loop error .You can't avoid this as both your old url and the new url are identical and can cause redirect loop if you remove the RewriteCond and Query perameter.
NE (no escape ) flag is important whenever you are redirecting to a fragment otherwise mod-rewrite converts the # to its hex %23 .
solution #2
RewriteEngine on
RewriteCond %{THE_REQUEST} !.*loop=no [NC]
RewriteCond %{THE_REQUEST} /division1/index\.php\?(.+)\s [NC]
RewriteRule ^ /division1/index.php?%1&loop=no#div [NE,L,R]
Clear your browser cache before testing these redirects.

Why won't my RewriteRules play nice together?

The first part of my code works and redirects/rewrites to new/file. Problem occurs when I try to pass variables. They all get redirected to new/file
Whatever variable I try to pass in file.php?foo=bar redirects to new/file instead of new/file/bar.
for example:
new/file rewrites to file.php
new/file/2 rewrites to file.php?page=2
file.php redirects to new/file
file.php?page=2 doesn't redirect to new/file/2 but get's overwritten to new/file instead
My code:
RewriteBase /domain.com
#regular
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file? [NS,R=301,L]
RewriteRule ^new/file?$ file.php?redirect=no [NS]
#with variable
RewriteCond %{QUERY_STRING} ^page=([0-9-]+)/?$
RewriteRule ^file\.php$ new/file/%1? [NS,R=301,L]
RewriteRule ^new/file/([0-9-]+)/?$ file.php?page=$1&redirect=no [NS]
Please note that I am passing more than just page variables.
I'm thinking maybe a QSA flag is supposed to go somewhere?
RewriteCond %{QUERY_STRING} !redirect=no
RewriteRule ^file\.php$ new/file [NS,R=301,L]
RewriteRule ^new/file$ file.php?redirect=no [NS]
In line 2 you had ?. This will replace the current querystring, and page=2 is lost.
In line 3 you had a ?. Paths don't contain questionmarks. Secondly the questionmark is interpreted as an "optional letter 'e'", because it is a RegEx. So "/new/fil" would also work (try it out).

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]

Resources