I need to write a rewrite rule to redirect from one domain to another.
maindomain.com/en/ -> secounddomain.com/en/
maindomain.com/en/pagename.html -> secounddomain.com/en/pagename.html
www.maindomain.com/en/ -> secounddomain.com/en/
www.maindomain.com/en/pagename.html -> secounddomain.com/en/pagename.html
secounddomain.com -> secounddomain.com/en/
www.secounddomain.com -> secounddomain.com/en/
and for secure (no back)
secounddomain.com/pl/ -> secounddomain.com/en/
www.secounddomain.com/pl/ -> secounddomain.com/en/
I try to do it like this but it doesn't work:
RewriteCond %{HTTP_HOST} ^maindomain.com/en/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
RewriteCond %{HTTP_HOST} ^www.maindomain.com/en/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
RewriteCond %{HTTP_HOST} ^www.secounddomain.com/pl/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
RewriteCond %{HTTP_HOST} ^secounddomain.com/pl/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
Best regards.
First of all this condition is wrong:
RewriteCond %{HTTP_HOST} ^maindomain.com/en/$
As %{HTTP_HOST} can only match host name hence it can match maindomain.com only.
Here is how your .htaccess should look like:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteRule ^(en(?:/.*|))$ http://secounddomain.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?secounddomain\.com$ [NC]
RewriteRule (?!^en/)^(.*)$ http://secounddomain.com/en/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?secounddomain\.com$ [NC]
RewriteRule ^pl(/.*|)$ http://secounddomain.com/en$1 [L,R=301,NC]
Related
I would like to implement followin in .htaccess file:
I have "dictionary", which tells me, which subfolder should be redirected where, i.e.
domain1.com/a -> domain2.com/x
domain1.com/b -> domain2.com/y
and everything else should be redirected to
domain2.com/z
That should be simple enough, but I can not figure it out.
My code:
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com/a$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/x [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com/b$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/y [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$ [NC]
RewriteRule ^(.*)$ %{ENV:proto}://domain2.com/z [L]
Variable %{HTTP_HOST} only matches domain not request uri.
You can use these rules:
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ - [env=proto:https]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ - [env=proto:http]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^a/?$ %{ENV:proto}://domain2.com/x [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^b/?$ %{ENV:proto}://domain2.com/y [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^ %{ENV:proto}://domain2.com/z [L,NC,R=301]
I need all subdomains to redirect back to the root.
Ex). test.website.ca -> https://www.website.ca
fake1.website.ca -> https://www.website.ca
www.ggg.website.ca/ -> https://www.website.ca
etc.
Here is the code that I tried:
RewriteCond %{HTTP_HOST} ^(.+)\.website\.ca$ [NC]
RewriteRule (.*) https://www.website.ca/$1 [L,R=301,QSA]
You can use:
RewriteCond %{HTTP_HOST} !^www\.website\.ca$ [NC]
RewriteRule ^ https://www.website.ca%{REQUEST_URI} [NE,L,R=301]
Without the uri, use same RewriteCond and:
RewriteRule ^ https://www.website.ca/ [L,R=301]
Those are the lines of concern currently in my .htaccess file,
note that i have two domains pointing to the same site:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R]
RewriteCond %{HTTP_HOST} ^example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.example2\.com$ [NC]
RewriteRule .* http://www.example.com/ [L,R=301]
Now I need to convert my site to SSL, I was instructed to use the following
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
I think that this code misses my other domain and the www rewrite part,
how to enable SSL while accounting for my other domain and also for the WWW part?
You can use:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^(www.)?example2\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]
I have several .htaccess rules I would like to combine. Here's the gist of what I want to accomplish:
jethro.mysite.com -> Redirects to -> gallery.mysite.com/?username=jethro&page_name=Home
(note: page_name is = Home if not already specified)
jethro.mysite.com/ajax/dir/script.php -> Redirects to -> gallery.mysite.com/ajax/dir/script.php
Anything else is processed like normal. Example: jethro.mysite.com/includes/blah.css -> Redirects to -> gallery.mysite.com/includes/blah.css
So far my .htaccess located at jethro.mysite.com looks like this, but I get an infinite loop:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^jethro.mysite.com [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://jethro.mysite.com/?username=jethro&page_name=Home [P]
RewriteCond %{QUERY_STRING} page_name=script
RewriteRule (.*) http://gallery.mysite.com/ajax/dir/script\.php [P]
RewriteCond %{QUERY_STRING} page_name=(.*)
RewriteRule (.*) http://gallery.mysite.com/?username=jethro&page_name=%1 [P]
If I disable the 5th and 6th lines above it will no longer do the loop. Please help how do I combine these ideas? Thanks!
Try the following.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^jethro.mysite.com [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule . http://jethro.mysite.com/?username=jethro&page_name=Home [P,L]
RewriteCond %{QUERY_STRING} page_name=script [NC]
RewriteRule . http://gallery.mysite.com/ajax/dir/script\.php [P,L]
RewriteCond %{QUERY_STRING} page_name=(.*) [NC]
RewriteCond %1 !=script [NC]
RewriteRule . http://gallery.mysite.com/?username=jethro&page_name=%1 [P,L]
I also assume that the [p] is intentional i.e. you want to proxy the request.
.htaccess:
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{QUERY_STRING} !^id=.
RewriteRule (.*) /index.php?id=%1 [L]
Expected behavior:
If there is a subdomain (test.example.com) it goes to the folder /service/ and if there isn't it goes to the folder /site/.
Example behavior:
test.example.com/post?id=2 -> public_html/service/post.php?id=1
test.example.com/category?id=3 -> public_html/service/category.php?id=3
example.com/register.php -> public_html/site/register.php
Try these rules:
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule !^service/ service/index.php?id=%1 [L]
RewriteCond %{HTTP_HOST} =example.com
RewriteRule !^site/ site%{REQUEST_URL} [L]