One rewrite rule or multiple ones in htaccess - .htaccess

I have a main domain, a couple of addon domains and recently went https. This is working ok for me but i would like your opinion:
## All http requests to the relative https url
RewriteCond %{SERVER_PORT} 80 [OR]
## redirect the other domains and the non www main main domain
RewriteCond %{HTTP_HOST} ^domain3.com [OR]
RewriteCond %{HTTP_HOST} ^www.domain3.com [OR]
RewriteCond %{HTTP_HOST} ^domain2.com [OR]
RewriteCond %{HTTP_HOST} ^www.domain2.com [OR]
RewriteCond %{HTTP_HOST} ^maindomain.com
## this stuff is for SSL
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
## i send them all to the https main domain
RewriteRule ^(.*)$ https://www.maindomain.com/$1 [L,R=301]
This is better approach to have a rewrite rule for each condition?

Related

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

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.

permanently redirect all pages of sub-domain to main domain .htaccess

I want to redirect all urls of sub-domain to main-domain, like m.abc-xyz.com/test1 to abc-xyz.com/test1 and m.abc-xyz.com/test2 to abc-xyz.com/test2 and so on....
I have this code in root .htaccess file which only works for m.abc-xyz.com to abc-xyz.com
RewriteCond %{HTTP_HOST} ^m\.abc\-xyz\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.m\.abc\-xyz\.com$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/cpanel-dcv/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?$ "https\:\/\/www\.abc\-xyz\.com" [R=301,L]
Most likely because you have [OR] only after first condition, so it's: first condition is true or all other are true (at the same time) which is not happening.
Try adding [OR] at all places where it should be.

.htaccess - Redirect all pages to https version of site and other domains

I currently have the following code within my .htaccess file. this ensures that my two other domains are redirected to my main URL. It also ensures that my current domain is redirected to self but with www should it have not been present.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.co\.uk [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.co\.uk [OR]
RewriteCond %{HTTP_HOST} ^newexample\.com
RewriteRule ^(.*)$ https://www.newexample.com/$1 [R=permanent,L]
My question is, how can i ensure that they all use https if it isn't already present. The above code seems to work unless someone types in www.newexample.com then it is not redirected to https.
You can use this rule:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk [OR]
RewriteCond %{HTTP_HOST} ^newexample\.com$
RewriteRule ^ https://www.newexample.com%{REQUEST_URI} [R=301,L,NE]
UPDATE:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk [OR]
RewriteCond %{HTTP_HOST} ^newexample\.com$
RewriteRule ^ https://www.newexample.com%{REQUEST_URI} [R=301,L,NE]

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

.htaccess force 404

Attempting to restrict access to Addon Domains through the Main Domain by forcing 404 but doesn't seem to be working, here's what I have:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteCond %{REQUEST_URI} ^/domain2.com/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/domain3.co.uk/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/test/(.*)$
RewriteRule ^(.*)$ - [L,R=404]
The directory test is 404'd correctly, so I'm thinking it's something to do with the .com and .co.uk characters?

Resources