.htaccess to require WWW for domain, but allow subdomain if existing with no hard coding - .htaccess

I'm trying to figure it out how to set up an .htaccess set of rules that would force the presence of the "www" in front of the domain if it is not initially specified, but at the same time, it will not have any effect if the an subdomain is pressent; all this without hard coding any domain name so that the script is portable around different servers and configurations.
EDIT:
I'm sorry I was not able to get this explained right in the fist place. So what I need is as follows:
http://example.com -> redirects to http://www.example.com
http://www.example.com -> does not redirect
http://subdomain.example.com -> does not redirect

This mod_rewrite rule should do it for second level domains:
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^example\.org
RewriteRule ^ http://www.example.org%{REQUEST_URI} [L,R=301]
This will not redirect subdomains like mail.example.org

Related

301 Redirect in htaccess from NON WWW to https://www with single redirect

I have seen couple of solutions to 301 redirect non www http i.e. http://domain-name to https://www.domain-name but all of them (in my experience) gives 2 redirects.
Either they first redirect from non www to www first and than http to https or in 2nd redirect first and than 1st redirect. One of the example of such code is:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The best solution would be one 301 redirect which takes care of both i.e. non www as well as http part. Can anyone please suggest me the right code.
Best rgds,
Jai
I am using this code and I get only one 301 redirect (to https://www.example.com) when I test http://example.com with httpstatus.io
# ensure www
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The first rule, if your address doesn't start with www will be redirected to the https + www (one redirect)
The second rule applies only if your address starts with a www (because of rule 1) and it doesn't contain https. (one redirect)
Indeed, your domain http://infocera.com is getting 2 redirects.
Using #MrWhite's suggestion about your domain going through Cloudflare: you should go to your domain in Cloudflare -> Rules -> Page Rules and delete any rule you have about redirecting.
I suggest to combine both rules into a single one by combining both conditions:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.example.com%{REQUEST_URI} [L,R=301]
That is simple, thus easy to understand for anyone maintaining the site.
The only drawback would be that you have to specify a fixed http host in the target rule. Which is perfectly fine except if you host multiple domains and hope to find a solution working for all of them at once.
I would generally advise against such an attempt though. Such general rules have nothing to do with an application logic, one should always prefer to implement such general rule in the actual http server's host configuration instead of using distributed configuration files (".htaccess"). That means that you have separate configurations for separate domains, which again allows to place such rule into the separate configuration files while still having only a single actual redirection that is performed.

htaccess rewrite all subdomains to url path

i need to redirect all request going to any subdomain on domain1.com to domain2.com passing the subdomain part as path, so for example:
sub1.domain1.com -> domain2.com/sub1
this works with my current rewrite rule:
RewriteCond %{HTTP_HOST} ^.+?\.domain1\.com$ [NC]
RewriteRule ^(.*)$ https://domain2.com/$1 [L,R=301]
but when i have a subdomain like the following, it doesn't work:
sub1.sub2.domain1.com -> is redirecting to domain2.com/ without taking the subdomain parts to the path. (domain2.com/sub1.sub2)
how can i get this to work for any combination of subdomain?
I fail to see who any such redirection currently works. The rewriting rule you posted certainly does not do what you describe...
I suggest such an approach to hand over the "subdomain name":
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:(.+)\.)?domain1\.com$ [NC]
RewriteRule ^ https://domain2.com/%1 [L,R=301]
https://sub1.sub2.domain1.com/some/path => https://domain2.com/sub1.sub2
This obviously will ignore whatever path has originally been requested, but that is what you intend, as I understood. If you also want to keep the path and only precede it with the subdomain name, then this variant should point you into the right direction:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:(.+)\.)?domain1\.com$ [NC]
RewriteRule ^/?(.*)$ https://domain2.com/%1/$1 [L,R=301]
https://sub1.sub2.domain1.com/some/path => https://domain2.com/sub1.sub2/some/path
It is a good idea to start out using a 302 redirection and only change that to a 301 once things work as desired. That prevents you from running into caching issues...

htaccess and https/non-www to https/www

I know there are many posts on redirects, and I've read a lot of them now, but only a few that I've found seem to touch on what I need and often seem to contain conflicting information
I need to get:
http://example.com -> https://www.example.com
http://www.example.com -> https://www.example.com
and more importantly:
https://example.com -> https://www.example.com
I can find many solutions to the first two. Like this one:
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
or this one:
RewriteEngine on
RewriteCond %{REQUEST_SCHEME} http [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R]
These rules look like what I should be after, but I've only got a certificate that covers https://www.example.com. Is the third redirect possible? (https://example.com to https://www.example.com). The answers in the question I linked seem to suggest it might be possible without another certificate, but there's disagreement.
I would have thought that there's no way around the problem, but I'm hoping there can be and would appreciate input.
*edit: if I don't get a certificate for https://example.com, it won't be classed as a duplicate by search engines will it?
To do a redirect between subdomains (* -> www), you need a certificate for each domain that runs https, or you need a wildcard certificate that handles them all.
The redirect is a completely separate issue.
Once you get a valid connection, you can redirect or not.
If the connection isn't valid (wrong/missing ssl cert), the redirect will never happen.

Htaccess rewrite rules to redirect multiple domains

I'm trying to redirect some domains correctly using the htaccess file. Here is what I got:
RewriteCond %{HTTP_HOST} ^special.com$
RewriteRule (.*) https://website.com/some-special-page [L,R=301]
RewriteCond %{HTTP_HOST} !^website.com$
RewriteRule (.*) https://website.com/$1 [L,R=301]
Why is special.com also redirecting to website.com and not to website.com/some-special-page ?
The overall redirect for all the other domains is working fine, however I can't figure out how to have this special case work (and not be overwritten by the second rule. Thanks in advance!

Subdomain adding a folder to .htaccess

I'm trying to redirect...
http://blog.example.org/folder/name-of-page.html
...to....
http://www.example.org/different-name-of-page.html
For my .htaccess file under blog.example.org I've added...
Redirect 301 /folder/name-of-page.html http://www.example.org/different-name-of-page.html
However, when I do this it takes me to http://www.example.org/blog/folder/different-name-of-page.html
For some reason it's auto-populating the /blog/folder/ part instead of taking me to the URL I've suggested.
Any idea on a fix?
It looks like it's interferring with rewrite rules that you have that map subdomains to folders. Use mod_rewrite instead of the Redirect directive.
Before any of your other rules, add:
RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} /folder/name-of-page\.html
RewriteRule ^ http://www.example.org/different-name-of-page.html [L,R]

Resources