Redirect All Subdomains to Root - .htaccess

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]

Related

how to redirect subdomain to non-www

I want to remove www. from start of my URL it tried this :
I have found this work for mydomain.com
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.mydomain\.com)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
If i use mydomain.net.in it doesn't work how to get it done?
RewriteCond %{HTTP_HOST} ^mydomain\.net\.in$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.mydomain\.net\.in)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
I am not sure why you first appending www and then removing try with below, it will work for both.
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
Clear cache beforehand.

Modifying current rewrite rules to force SSL

I want all of the pages on my website to force an SSL connection. Currently I have this in place...
#RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^(www)\.domain\.com$ [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The above rules were inspired by the internet and I do not fully understand them. I tried changing the last line to RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] (note the https part).
When I test the redirect using http://www.domain.com/path-to-here I am redirected to https://www.domain.com/.
In short, the ultimate goal is to force www and https while preserving all original paths.
Ex:
http://domain.com -> https://www.domain.com
http://domain.com/this-is-my-path-1/ -> https://www.domain.com/this-is-my-path-1/
https://domain.com -> https://www.domain.com
https://domain.com/this-is-my-path-1/ -> https://www.domain.com/this-is-my-path-1/
http://www.domain.com -> https://www.domain.com
http://www.domain.com/this-is-my-path-1/ -> https://www.domain.com/this-is-my-path-1/
You can use:
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NE,R=301,L]

htaccess redirect to enother domain and language

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]

How to redirect example.com/folder/ to www.example.com/folder/?

I need to know how to redirect example.com/folder/ to www.example.com/folder/ using htaccess. At moment htaccess redirects example.com/folder/ to www.example.com. This is what htaccess says at moment:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder$ /folder/ [L,R]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder/(.*) /$1 [L]
What changes are needed?
Just use this condition:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
First check that the host value is not empty (may be with HTTP/1.0).
The check if we already "use" www and then chrck for SSL (https)
If you want to keep subdomains intact, you should change the second line to this (heads up this is not 100% generic since you have to hardcode in your domain!):
RewriteCond %{HTTP_HOST} ^example.com [NC]

How can I make redirects based on subdomains in .htaccess?

.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]

Resources