htaccess language rediretion - .htaccess

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]

Related

Htaccess www to non-www but keep the url intact

I think that this is rather simple, but I just can't wrap my head around it.
I have a domain like https://www.example.com
I want to rewrite it to https://example.com
That works. But I am also rewriting other urls like https://www.example.com/privacy
If I try to rewrite these too the user is always redirected to https://example.com/privacy.php
How do I prevent my server to show the actual filename instead of its rewritten url?
My code looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^checkout checkout.php [L]
RewriteRule ^imprint imprint.php [L]
RewriteRule ^privacy privacy.php [L]
RewriteRule ^terms terms.php [L]
RewriteRule ^allergy allergy.php [L]
RewriteRule ^nutrition nutrition.php [L]
RewriteRule ^order/([A-Z]*)?$ confirmation.php?lang=&id=$1 [L,QSA]
Any help is appreciated.
Regards
Have it this way:
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(checkout|imprint|privacy|terms|allergy|nutrition)/?$ $1.php [L]
RewriteRule ^order/([a-z]+)/?$ confirmation.php?lang=&id=$1 [L,QSA,NC]
Make sure to test after clearing your browser cache. Problem appears to be due to your patterns not using end anchor.
I have combined your multiple similar rules into one rule.

How to redir subfolders to different domain than root domain

IHi, I'm to this, I've read and tried all tips I've found here, but it's still not working the way I need.
I'm using htaccess to redir domain and it's structure to new domain:
`rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]`
It redirs all subfolders e.g. www.olddomain.com/contact/ to www.newdomain.com/contact/ etc., so far so good, BUT I need to redir the mainpage, the one page only, to different URL:
from www.olddomain.com to www.newdomain.com/about-old/
All I've tried this:
`Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/about-old/ [r=301,nc,L]
rewritecond %{http_host} ^www.olddomain.com [nc]
rewriterule ^(.*)$ http://www.newdomain.com/$1 [r=301,nc]`
but it just redirs EVERYTHING to www.newdomain.com/about-old/
Any ideas, please?
Thanks you
To match home page pattern you need is:
^/?$
So your full code will be:
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^(www\.)?olddomain\.com$ [nc]
rewriterule ^/?$ http://www.newdomain.com/about-old/ [R=301,L]
rewritecond %{http_host} ^(www\.)?olddomain\.com$ [nc]
rewriterule ^(.+)$ http://www.newdomain.com/$1 [R=301,L,NE]

Redirecting a web folder directory to another htaccess

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]

redirecting from short url

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

disabled htaccess for selected directory

i have a htaccess code shown below:
RewriteEngine On
RewriteCond $1 !^/administrator
RewriteCond %{HTTP_HOST} ^localhost/host
RewriteRule (.*) http://localhost/host/$1 [R=301,L]
RewriteRule ^([-\w]+)?/?([-\w]+)?/?([-\w]+)?/?$ index.php?seg1=$1&seg2=$2&seg3=$3
but it still read the htaccess in administrator folder. is the code incorrect?
Thanks.
RewriteCond statements apply to one RewriteRule only.
You need to repeat your RewriteConds for the second rewrite rule (and use REQUEST_URI):
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/administrator
RewriteCond %{HTTP_HOST} ^localhost/host
RewriteRule (.*) http://localhost/host/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/administrator
RewriteCond %{HTTP_HOST} ^localhost/host
RewriteRule ^([-\w]+)?/?([-\w]+)?/?([-\w]+)?/?$ index.php?seg1=$1&seg2=$2&seg3=$3

Resources