I have an apache server with multiple websites hosted:
main-website.com
subdomain1.main-website.com
subdomain2.main-website.com
another-website1.com
another-website2.com
another-website3.com
I need to redirect only https://www.main-website.com to https://www.main-website.com
The subdomains and all the other websites don't need a ssl certificate; therefore I want to exclude them from redirection by specifying that only main-website needs to be redirected.
This is my .htaccess syntax (it seems correct having researched a lot on this topoic)
#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The condition of redirecting main-website.com has been specified, but it does not work! In fact every other domain and subdomain is redirected to HTTPS!!
Do you know where the error could be?
Thank you :)
There must be a typo in your question, you want to redirect from https://www.main-website.com to https://www.main-website.com I will take for granted you wanted to say http://www.main-website.com to https://www.main-website.com
So, your first RewriteCond-RewriteRule set rewrites:
somesite.com ---> https://www.somesite.com
But notice that you also force https in that rediction. Therefore any attempt to reach http://somesite.com will go to https://www.somesite.com
It is probably this rule set that is triggered by your other domains. That first rule should be to rewrite: non www to www, without forcing https at that point.
Then the next rule set will apply only to your main-website and send it to https.
#NON-WWW to WWW (whis applies to all domains and subdomains)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect HTTP to HTTPS only for main-website.com:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} main-website.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I have updated the first set of instructions. Still, it does not work :(
Related
I have an .htaccess that I've cobbled together over the years using this site as a guide but hoping I can consolidate some of the lines to make it more efficient.
These lines do three things:
Redirect any non-www requests to the www variant;
Redirect any .com requests to the proper .org TLD;
Redirect any http to the proper https variant; 3.
Here's what I have:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine On
RewriteCond %{http_host} ^example.com
RewriteRule ^(.*) https://www.example.org/$1 [R=301,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} example\.org [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R,L]
Is there a way to combine (at least some) of these functions into fewer lines?
I'm NOT having any problems, it's just the organizer in me that feels it could be neater.....
Thanks in advance for any suggestions!
# Rule #1
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Rule #2
RewriteCond %{http_host} ^example.com
RewriteRule ^(.*) https://www.example.org/$1 [R=301,L]
# Rule #3
RewriteCond %{HTTP_HOST} example\.org [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.org/$1 [R,L]
Yes, your rules can be optimised. Not least because they don't actually do what you say they do!
(I'm assuming you have no intention to implement HSTS?)
Redirect any .com requests to the proper .org TLD;
These directives don't actually redirect the .com variant to .org, as you suggest in your 2nd requirement. If you are seeing this redirect then it's not because of these directives. Most probably WordPress itself is doing this, later in the request.
For example...
Scenario #1:
Request http://www.example.com/ (or https://www.example.com/)
No redirects occur since the requested host does not match rule #1, #2 or #3
The request is not redirected to .org (or HTTPS in the case of a request to http://...)
scenario #2:
Request http://example.com/ (or https://example.com/)
Redirect to https://www.example.com/ (same domain) by rule #1
No further redirects occur as stated in "Scenario #1" above.
The request is not redirected to .org.
As you can see from these examples your rule #2 (that matches example.com only) doesn't actually do anything. It is always bypassed because rule #1 has already redirected the request to the www subdomain. It would be "better" if rule #1 and #2 were reversed, however, that still wouldn't resolve the issue when www.example.com (www subdomain) was requested, as nothing would happen still.
Other notes:
Rule #3 - HTTP to HTTPS redirect - is a temporary (302) redirect. This should be a 301, like the others.
No need to repeat the RewriteEngine On directive. It only needs to occur once in the file. (It is often seen repeated when .htaccess files are edited by "the machine", not code "by hand".) For readability, this should be at the top of the file. However, if you have multiple RewriteEngine directives it's actually the last instance that wins and controls the entire file (which may not be intuitive). eg. If you put a RewriteEngine Off directive at the very end of the .htaccess file then... it's off for the whole file, despite any RewriteEngine directives you might have preceding this.
Simplified (and "fixed"):
Redirect any non-www requests to the www variant;
Redirect any .com requests to the proper .org TLD;
Redirect any http to the proper https variant; 3
These 3 requirements are only satisfied if everything redirects to www + .org + HTTPS. ie. Everything must redirect to https://www.example.org/ (the canonical URL).
So, your existing 3 rules can be reduced to a single rule, in order to satisfy your 3 requirements. ie. If the request is not for https://www.example.org/ then redirect to https://www.example.org/.
RewriteEngine On
# If not the canonical host OR not HTTPS then redirect to HTTPS + canonical host
RewriteCond %{HTTP_HOST} !^www\.example\.org$ [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.example.org/$1 [R=301,L]
We don't need to reference example.com at all here, since we are only concerned whether it is not the canonical host.
And, since this is a WordPress site, this redirect must go before the WP front-controller, near the top of your .htaccess file.
You won't need to repeatedly write RewriteEngine On. Once is enough.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ https://www.example.org/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have multiple domains that end with .nl .com and .de. For each domain name I have a different language. My files are on the .nl domain. What I would like is the following for all domains:
Redirect http://* to https://*
Redirect http://example.* to https://www.example.*
I Have the files on 1 server, and want them to look like it is on 3.
This is what I have on the main server (.NL)
RewriteCond %{HTTP_HOST} !www.example.nl$ [NC]
RewriteRule ^(.*)$ https://www.example.nl/$1 [L,R=301]
And this for the .DE
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.de/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !www.example.de$ [NC]
RewriteRule ^(.*)$ http://www.example.de/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.nl/$1 [P]
And this for the .COM
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^(.*)$ http://www.example.nl/$1 [P]
I've tried everything, but I could not figure it out.
# This rule will redirect users from their original location, to the same location but using HTTPS.
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://www.example.nl/$1 [R=301,L]
#primary site redirect
RewriteCond %{HTTP_HOST} !^www\.example\.nl [NC]
RewriteRule (.*) https://www.example.nl/$1 [R=301,L]
I believe that's all that's required, as you can see, you first redirect ALL requests that are not https to https example.nl, then you redirect anything that is NOT www.example.nl to https://www.example.nl
If I'm guessing wrong about what you need with [P] you can just replace I think [R=301,L] with [P], though I've never dealt with that configuration so I can't say for sure, but it's easy to test.
https://wiki.apache.org/httpd/RewriteHTTPToHTTPS
this is more or less what we use for https. I think you're getting confused and not realizing that all you need to know is that something is NOT example.nl/$1, there's no need to list all those nots, since all of them are not example.nl/$1
Note the following, which are all NOT the second rule, all will get redirected:
https://example.nl
https://www.example.com
https://example.de
https://www.example.de
Since these are all not:
https://www.example.nl
they get redirected. (.*) means everything, including /.
There's a subtle thing that you need to be aware of with https, you have to have a certificate for ALL versions of all the domains, that's www/non www, or you get those browser alert popups for any initial domain/subdomain that does not have certificate support when the page initially loads. For example, if you don't have a certificate for example.com but you have one for www.example.com if a url comes in: http://example.com you'll get that browser alert, which is very bad for usability and makes it look like it's a scammer site.
I can't remember the exact ordering of processing, but I think that's how it works.
After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
My requirements are as follows:
Certain pages of my site need to use https
The rest can use http
I also need to redirect everything to use a www. prefix (e.g., if someone visits domain.com it redirects to www.domain.com)
I've tried a number of solutions listed here on Stack Overflow, but none seems to work.
Here's the relevant portion of my htaccess file:
#force https for certain pages
RewriteCond %{HTTPS} !=on
RewriteRule ^(login2\-test\.php.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
#redirect all pages to www
RewriteCond %{HTTPS} !=off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Redirecting all pages to www works fine. However, the redirect of login2-test?Switch=ftr (which is in the reg/ subdirectory) to https results in no match, so the page is displayed using http.
I've rewritten that line as follows:
RewriteRule ^(reg\/)(login2\-test\.php.*)$ https:www.domain.com/$1$2 [L,R]
This at least matched / redirected to https, however the browser could not resolve it.
I've swapped the order of the rules (e.g., www redirect first); that didn't help.
I would appreciate any help -- I've been struggling with this for a while now.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
We're using a plesk based system and just recently changed domain names. I'd like to redirect all requests coming in to the old domain to the new. There are many question asked in a similar fashion but mine is a bit different. I'd like to ensure that all subdomains get routed to the same subdoamin on the new domain. I set up a generic htaccess in the docroot but for some reason it is also applying to all subdomains.
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
How can I make this more general so that subdomains also appropriately get routed? For bonus points, how can I route https requests to https and http to http.
I'd like to add that the rule transforms the first url to the second which is not desirable:
http://SUBDOMAIN.olddomain.com/somepath/somefile.php
http://newdomain.com/subdomains/SUBDOMAIN/httpdocs/somepath/somefile.php
The proper transform should create the following url:
http://SUBDOMAIN.newdomain.com/somepath/somefile.php
Lastly, this should work with wildcard subdomains.
Thanks in advanced!
Try:
# ignore "www."
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [L,R=301]
You'd want this before the rule that you already have.
And since you've edited your question, you want an additional internal rewrite for handling subdomains in general on your new site, this has nothing to do with the redirect. This is brand new functionality that is outside of the scope of a 301 redirect. You're going to need special rules to handle internal routing of subdomains:
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.newdomain.com$ [NC]
RewriteRule ^(.*)$ /subdomains/%1/httpdocs/$1 [L]