htaccess redirect non-existed subdomain to main domain and main domain still accessible - .htaccess

I'm trying to set htaccess redirect non-existed subdomain to main domain.
I already tried these rules, but the rule still not work.
Non-existed subdomain is not redirected to main domain.
RewriteCond %{HTTP_HOST} !^www\.website\.id$ [OR]
RewriteCond %{HTTP_HOST} !^official\.website\.id$ [OR]
RewriteCond %{HTTP_HOST} !^admin\.website\.id$ [OR]
RewriteCond %{HTTP_HOST} !^support\.website\.id$
RewriteRule ^ https://website.id [R=301,L]
or
RewriteCond %{HTTP_HOST} !^www\.website\.id$ [NC]
RewriteCond %{HTTP_HOST} !^official\.website\.id$ [NC]
RewriteCond %{HTTP_HOST} !^admin\.website\.id$ [NC]
RewriteCond %{HTTP_HOST} !^support\.website\.id$ [NC]
RewriteRule ^ https://website.id [R=301,L]
or
RewriteCond %{HTTP_HOST} !^((www|officia|admin|support)\.website\.id)?$ [NC]
RewriteRule ^(.*) https://website.id/$1 [R=301,L]
When I change the rule to
RewriteRule https://website.id [R=301,L]
non-existed subdomain can be redirected to main domain.
But sadly, the main domain is not accesible.
I really new in this htaccess settings.
thanks.

Related

How do I write these htaccess rules as one rule instead of multiple?

I have these rules in my htaccess. one is for the main domain of the server and the others are other domains that are pointed at the server.
the purpose is to redirect subdomains to a url that is written a certain way. the main domain excludes certain subdomains from the redirect.
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.maindomain\.com [NC]
RewriteRule ^(.*)$ https://maindomain.com/index/parser.php?%2&domain_name=maindomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.anotherdomain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/index/parser.php?%2&domain_name=anotherdomain.com [QSA,L]
RewriteCond %{HTTP_HOST} ^(.*)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-.]+)\.whatever\.com [NC]
RewriteRule ^(.*)$ https://whatever.com/index/parser.php?%2&domain_name=whatever.com [QSA,L]
I don't want to keep having to add a new 3 lines for each new domain pointed at the server. How do I create one rule set to incorporate any domain that is pointed at the server? I would still need certain subdomains to be excluded from the main domain, but it wouldn't matter if the same subdomains are excluded from all the domains.
here are a couple things I tried. neither works.
#attempt 1
RewriteCond %{HTTP_HOST} ^(?:www\.|)([a-z0-9-]+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
#attempt 2
RewriteCond %{HTTP_HOST} !^(www|dev|mydev|something|somethingelse)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]
Not sure if I solved it, but this seems to be working. not sure if i can make it even better to handle http and https at the same time, and to handle many exceptions for main domain but just the one exception for the rest, but this is satisfactory enough for my needs. most domains will be ssl, so only need to edit htaccess files to add non ssl domains.
RewriteEngine On
#remove www from any domain pointed at box whether it's ssl or not
RewriteCond %{HTTP_HOST} ^www\.
RewriteCond %{HTTPS}s ^on(s)|off
RewriteCond http%1://%{HTTP_HOST} ^(https?://)(www\.)?(.+)$
RewriteRule ^ %1%3%{REQUEST_URI} [R=301,L]
#main domain with subdomain exceptions
RewriteCond %{HTTP_HOST} ^(.+)\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^dev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mydev\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^something\.maindomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^somethingelse\.maindomain\.com$ [NC]
RewriteRule ^ https://maindomain.com/index/parser.php?%1&domain_name=maindomain.com [L,R]
#non ssl domains here. three lines each.
RewriteCond %{HTTP_HOST} ^(.+)\.anotherdomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.anotherdomain\.com$ [NC]
RewriteRule ^ http://anotherdomain.com/index/parser.php?%1&domain_name=anotherdomain.com [L,R]
RewriteCond %{HTTP_HOST} ^(.+)\.whatever\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\.whatever\.com$ [NC]
RewriteRule ^ http://whatever.com/index/parser.php?%1&domain_name=whatever.com [L,R]
#all ssl domains use this one rule.
RewriteCond %{HTTP_HOST} ^(.+)\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteCond %{HTTP_HOST} !^www\.(.+\.(?:[a-z]{2}\.[a-z]{2}|[a-z]+))$ [NC]
RewriteRule ^(.*)$ https://%2/index/parser.php?%1&domain_name=%2 [QSA,L]

htaccess Redirect non.www and www. to a subfolder

I tried different ways in htaccess but none worked. I have 2 domains and 1 webshosting.
I want that any user who types in website_1.com or www.website_1.com should be redirected to www.website_1.com/abc and any user who types website_2.com or www.website_2.com should be redirected to www.website_2.com/xyz
How could I put that in one single htaccess file? My current htaccess file:
#Redirect from non-www. to www.
RewriteCond %{HTTP_HOST} ^website_1\.com$ [NC]
RewriteRule ^(.*)$ http://www.website_1.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^website_2\.com$ [NC]
RewriteRule ^(.*)$ http://www.website_2.com/$1 [R=301,L]
#Redirect specific Domain to subfolder
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website_1\.com [NC]
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule ^(.*)$ /abc/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website_2\.com [NC]
RewriteCond %{REQUEST_URI} !^/xyz/.*$
RewriteRule ^(.*)$ /xyz/$1

Three 301 redirect

hope some could help me.
I have a wordpress blog with 3 domains, .com, .es and .net. The .com is the target.
And i want to redirect from non-www to www as well.
The redirect from .es to .com works fine, and .net to .com too. But when i add the redirect from non-www to www is not working and the site doesnt load.
This is my .htaccess file...
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^myblog.com [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]
#RewriteCond %{HTTP_HOST} ^myblog.es$ [OR]
#RewriteCond %{HTTP_HOST} ^www.myblog.es$
#RewriteRule ^(.*)$ "http\:\/\/www\.myblog\.com/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^myblog\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.myblog\.es$
RewriteRule ^(.*)$ "http\:\/\/myblog\.com\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^myblog.net [NC,OR]
RewriteCond %{HTTP_HOST} ^www.myblog.net [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]
These rules should suffice for your needs:
RewriteEngine On
# redirect myblog.es and myblog.net (with or without www.) to www.myblog.com
RewriteCond %{HTTP_HOST} ^(www\.)?myblog\.(es|net)$
RewriteRule ^(.*)$ http://www.myblog.com/$1 [R=301,L]
# Force www on myblog.com
RewriteCond %{HTTP_HOST} ^myblog.com [NC]
RewriteRule ^(.*)$ http://www.myblog.com/$1 [L,R=301,NC]

.htaccess redirect from subdomain's articles to main domain without subdomain url change

I have a domain, www.example.com and subdomain test.example.com. When i call test.example.com i need to redirect this to www.example.com/search/property/test_state without changing the url from test.example.com. so the url should look like test.example.com only.
I did this with the .htaccess code like,
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule ^$ /search/property/%1_state [P]
Now after this when i access any urls from the subdomain like test.example.com/content/sell_your_home it should not change the url to main domain.
currently when i access this it is redirecting to main domain. any ideas?
If the test subdomain has the same server root (document root) as the main domain, then you don't need the P flag:
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^$ /search/property/%1_state/ [L]
Otherwise, you need the full URL:
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^$ http://www.example.com/search/property/%1_state/$1 [L,P]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^webmail\.example\.com
RewriteCond %{HTTP_HOST} !^m\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,P]

request uri redirect ignore subdomain

I have the htaccess code below being used to redirect mobile devices to the mobile page of a site. However, I need it to not redirect if it is a subdomain URL; ex: subdomain.mydomain.com
What change(s) do I need to make?
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/mydomain.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ https://mydomain.com/mobile-page.html [L,R=302]
Add these conditions:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} !^(.+)\.mydomain\.com$ [NC]
So that in orde for the redirect to occur, the requested host doesn't start with a www, and it has a subdomain in front of mydomain.com (that's not www, via the first condition). So you're rules will look something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain.*$
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ https://mydomain.com/mobile-page.html [L,R=302]

Resources