We are moving to another domain and want to redirect all old traffic:
en.site1.com/whatever >>> en.site2.com/whatever
cz.site1.com/whatever >>> cz.site2.com/whatever
fr.site1.com/whatever >>> fr.site2.com/whatever
# ...and so on
We are currently using .htaccess for redirects, so we would prefer a solution using that. RewriteRule itself doesn't see the domain name (*.example.com), so we have to use RewriteConds:
RewriteCond %{HTTP_HOST} en.site1.com
RewriteRule ^(.*)$ http://en.site2.com/$1 [R=301]
RewriteCond %{HTTP_HOST} cz.site1.com
RewriteRule ^(.*)$ http://cz.site2.com/$1 [R=301]
RewriteCond %{HTTP_HOST} fr.site1.com
RewriteRule ^(.*)$ http://fr.site2.com/$1 [R=301]
# ...and so on
Doing this for each of X supported languages is overly verbose. I imagine a solution that would use a regex or something, instead of listing all languages:
# $0 is the regex match from RewriteCond
RewriteCond %{HTTP_HOST} ^(.*).site1.com
RewriteRule ^(.*)$ http://$0.site2.com/$1 [R=301]
Is that possible? Or is there any other way in .htaccess that doesn't include extensive copypasting?
You can capture regex variables from RewriteCond in much the same way as you can from RewriteRule.
The difference in syntax, when you back-reference the captured variables, is that you use % instead of $.
Eg. Instead of:
RewriteCond %{HTTP_HOST} en.site1.com
RewriteRule ^(.*)$ http://en.site2.com/$1 [R=301]
RewriteCond %{HTTP_HOST} cz.site1.com
RewriteRule ^(.*)$ http://cz.site2.com/$1 [R=301]
RewriteCond %{HTTP_HOST} fr.site1.com
RewriteRule ^(.*)$ http://fr.site2.com/$1 [R=301]
you can use:
RewriteCond %{HTTP_HOST} ([a-z]{2}).site1.com
RewriteRule ^(.*)$ http://%1.site2.com/$1 [R=301]
For Multiple sub domains to redirect particular specific path redirection.
Please add below code in your .htaccess file.
RewriteCond %{HTTP_HOST} abc.com$ [NC]
RewriteRule ^ abcd.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ab.com$ [NC]
RewriteRule ^ abs.com%{REQUEST_URI} [R=301,L,NE]
Related
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]
I have a set of mod-rewrite rules which look like this:
RewriteCond %{HTTP_HOST} AAAexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.AAAexample.com/folder/AAA [R=301,L]
RewriteCond %{HTTP_HOST} BB-BBexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.BB-BBexample.com/folder/BB-BB [R=301,L]
RewriteCond %{HTTP_HOST} CCCCCexample.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://www.CCCCCexample.com/folder/CCCCC [R=301,L]
Essentially I have a set of domains matching (PATTERN)example.com, where (PATTERN) can contain any combination of letters and hyphens.
Is there a way to condense the above rules into a single set such that I Pull PATTERN as a dynamic variable into the final rewrite URL?
Thanks!
You can use:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)(example\.com)$ [NC]
RewriteRule ^/?$ http://www.%1%2/folder/%1 [R=301,L]
I have several domains pointing to the same webspace (www.domain1.com, www.domain2.com, www.domain3.com, etc.). Only when one domain is used, e.g. www.domain1.com, I want to redirect users from different short link, e.g. www.domain1.com/link or www.domain1.com/link2, to another URL.
I have put together the following. The RewriteCond is probably ok but the RewriteRules doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteRule ^/test$ http://www.domain1.com/xyz.php [R=301]
RewriteRule ^/test2$ http://www.domain1.com/abc.php [R=301]
RewriteRule ^/test3$ http://www.domain1.com/abc/test10.php [L,R=301]
Do you have any tip how the correct RewriteRules should look like?
Hopefully this helps you get going in the right direction...
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^(/test/)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/xyz.php [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^(/test2/)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/abc.php [R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com [NC]
RewriteCond %{REQUEST_URI} ^(/test3/)$ [NC]
RewriteRule ^(.*)$ http://www.domain1.com/abc/test10.php [L,R=301]
Let's say short.com is the short domain and long.com is the long domain
updated:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.long.com
RewriteRule (.*) http://long.com/ [R=301]
RewriteCond %{HTTP_HOST} ^short\.li$ [NC]
RewriteCond %{REQUEST_URI} !^/redirect
RewriteRule ^(.*)$ /redirect?short=$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)short\.li$
RewriteRule ^$ http://long.com/ [L,R=301]
both domains point to that root directory. When I type short.li I end up on long.com/?l=
how did I manage to screw up like that?^^
Try this in your htaccess file :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)short\.com$
RewriteRule ^$ http://www.long.com/ [L,QSA,R=301]
Remove RewriteEngine on if it is already there
I think you might want something along the lines of this:
RewriteEngine on
RewriteCond {REQUEST_URI} !/
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/redirect.php?short=$1 [L,R=301]
RewriteCond {REQUEST_URI} /
RewriteCond {HTTP_HOST} short.com
RewriteRule ^(.*) http://long.com/ [L,R=301]
Not sure if the regex is needed on that last one or not, but something like this should work
I'm trying to redirect one domain to the root and another to a directory. The problem I'm having is the second domain is overwriting the firsts redirection.
Here is what I have.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^http://example2.com [NC]
RewriteCond %{HTTP_HOST} !^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1
That's because 'example2.com' and 'www.example2.com' are not 'example.site.net', like your .htaccess rule states...
If you want those rules to apply to those specific domains, you need to remove the '!' in front of them. Otherwise you need to explain what specific domains need to point to what, not just 'one domain' and 'the other domain'.
Edit: Also, you're not supposed to include the 'http://' for your HTTP_HOST conditions.
I think you might not realize you're negating your comparisons with "!"
Also, you dont' have to turn the engine on twice.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://example.site.net$ [NC]
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [L]
RewriteCond %{HTTP_HOST} ^http://example2.com [NC]
RewriteCond %{HTTP_HOST} ^http://www.example2.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{REQUEST_URI} !^/example2_directory/
RewriteRule ^(.*)$ /example2_directory/$1