i would like to redirect 3 different urls, all in one htaccess file. But at the moment it isn't working and only the first rule is always triggered.
I would like to redirect the following:
service.abc.info to www.abc.de
service.abc.info/feedback/abc/ to feedback.abc.info/abc/
service.abc.info/feedback/def/ to feedback.abc.info/def/
For 2. and 3. I also need to redirect the complete Request URI.
Here is my .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule .* http://www.abc.de [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info/feedback/abc/$ [NC]
RewriteRule .* http://feedback.abc.info/abc/{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info/feedback/def/$ [NC]
RewriteRule .* http://feedback.abc.info/def/{REQUEST_URI} [R=301,L]
The %{HTTP_HOST} variable holds the contents of the Host: request header, which just contains a hostname. No URI paths. You'll need to match the paths in the pattern of the rewrite rule, additionally, since the first rule is a superset of the other two, you need to move that to the end.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^/?feedback/abc/(.*)$ http://feedback.abc.info/abc/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^/?feedback/def/(.*)$ http://feedback.abc.info/def/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^service\.abc\.info$ [NC]
RewriteRule ^(.*)$ http://www.abc.de/$1 [R=301,L]
Related
Im trying to redirect several urls with 3 parameters to different static urls with .htaccess but nothing working.
1.
http://olddomain.com/index.php?id_category=28&controller=category&id_lang=2
to
https://newdomain.com/page1/
http://olddomain.com/index.php?id_category=30&controller=category&id_lang=2
to
https://newdomain.com/page2/
http://olddomain.com/index.php
to
https://newdomain.com
I tried the below code but http://olddomain.com/index.php not going to https://newdomain.com :
RewriteCond %{HTTP_HOST} ^olddomain.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301,NC]
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$
RewriteRule ^index.php$ https://newdomain.com/page2/? [R=301,L]
You need to have specific longer matches first and then have rules to remove index.php or domain redirect:
RewriteEngine On
# specific redirects with index.php as optional match
RewriteCond %{QUERY_STRING} ^id_category=28&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page1/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^id_category=30&controller=category&id_lang=2$ [NC]
RewriteRule ^(index\.php)?$ https://newdomain.com/page2/? [R=301,L,NC]
# remove index.php and redirect to newdmain
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^(?:index\.php/?)?(.*)$ https://newdomain.com/$1 [L,R=301,NC,NE]
Make sure to clear your browser cache before testing this change.
In case you are taking page's id from id_lang= variable then please try following rules. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules to redirect to link: https://newdomain.com/page1/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\?id_category=28&controller=category&id_lang=(\d+)\s [NC]
RewriteRule ^ https://newdomain.com/page%1/? [NE,R=301,L]
##Rules to redirect https://newdomain.com/ here.
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/index\.php\s [NC]
RewriteRule ^ https://newdomain.com [NE,R=301,L]
I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]
Sorry this has no doubt been asked multiple times before, I just want clarification that the following code will redirect any url on olddomain.com to the newdomain.com homepage not the equivalent url:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Also if I wanted any subdomain on olddomain.com eg.subdomain.olddomain.com to go to the homepage of newdomain.com what would I have to do? Can I use a universal selector or would I have to write a condition for each subdomain like so:
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
Both of attempts are not correct as first will redirect:
http://olddomain.com/foobar to http://newdomain.com/foobar
not to the homepage of newdomain. Same is the problem with 2nd rule.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.olddomain\.com$ [NC]
RewriteRule ^ http://subdomain.newdomain.com/ [R=301,L]
I want the user to be redirected whenever he reaches my subdomain
Here is what is inside my htaccess:
RewriteEngine On
RewriteRule ^http://smale.deals.com/(.*) http://traual.deals.com/$1 [R=301,L]
RewriteRule ^http://deals.com/smale/(.*) http://deals.com/traual/$1 [R=301,L]
But no redirect happens. why?
I also have got this in my root htaccess:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} (Android|iPhone|iPod|Blackberry) [NC]
RewriteCond %{REQUEST_URI} !^/mobile [NC]
RewriteRule ^(.*)/?$ mobile/$1 [L]
You cannot include the protocol and domain in RewriteRule. Those need to be accounted for in RewriteCond:
RewriteEngine On
# Rewrite requests to smale.deals.com to traual.deals.com
RewriteCond %{HTTP_HOST} ^smale\.deals\.com$ [NC]
RewriteRule (.*) http://traual.deals.com/$1 [R=301,L]
# For deals.com...
RewriteCond %{HTTP_HOST} ^deals\.com$ [NC]
# Rewrite requests to smale/ to deals.com/traual/
RewriteRule ^smale/(.*) http://deals.com/traual/$1 [R=301,L]
I have a small htaccess file, which contains some simple rules.
First I want to redirect or to add www at the begin of the main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|serv)\.(.*\-.*)\.com [NC]
RewriteRule ^(.*) http://www.maindomain.com%{REQUEST_URI} [L,R=301]
In the second step I want to remove the folder serv from the URI.
I have following folder structure:
www.maindomain.com
serv/
content of the main web page
To do that I have following rules:
RewriteCond %{HTTP_HOST} ^serv.* [NC]
RewriteCond %{REQUEST_URI} !^/serv.* [NC]
RewriteRule ^(.*) /serv/$1 [L,QSA]
This works so far, but my own rewrite rules conflicts with the conditions and rules above:
RewriteEngine On
RewriteBase /
RewriteRule ^search/$ search.php [L,QSA]
So my URI get sometimes something like that www.maindomain.de/search/search.
Here is my full htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|serv)\.(.*\-.*)\.com [NC]
RewriteRule ^(.*) http://www.maindomain.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^serv.* [NC]
RewriteCond %{REQUEST_URI} !^/serv.* [NC]
RewriteRule ^(.*) /serv/$1 [L,QSA]
RewriteEngine On
RewriteBase /
RewriteRule ^search/$ search.php [L,QSA]
What is wrong? I don't find the mistake.
Furthermore I want to know if it is possible, if the subdomain isn't serv and the URI doesn't contain serv that the subdomain will be redirected to www.
if it is possible, if the subdomain isn't serv and the URI doesn't contain serv that the subdomain will be redirected to www
For the last part of your question, add the following to your .htaccess file
#if the subdomain is not serv
RewriteCond %{HTTP_HOST} !^serv\.[^\.]+\.de$ [NC]
#and the uri does not contain serv
RewriteCond %{REQUEST_URI} !^.*serv.*$ [NC]
#redirect to www
RewriteRule .* http://www.maindomain.de%{REQUEST_URI} [NC,L,R=301]
Edit:
I replaced %1 reference with the host Above, which you can change to match your actual host.
For the first part of your question, your rule below will match any host that does not begin with www. or serv. that does not have a dash in the domain i.e. it will match www.maindomain.com because it does not have a '-`.
RewriteCond %{HTTP_HOST} !^(www|serv)\.(.*\-.*)\.com [NC]
This will result in an infinite redirect as you observed.
To fix it change it to the following
#if host is NOT www.anything.com or serv.anything.com
RewriteCond %{HTTP_HOST} !^(www|serv)\.[^\.]+\.com$ [NC]
#redirect to www.maindomain.com
RewriteRule .* http://www.maindomain.com%{REQUEST_URI} [L,R=301]