How can I achieve all of this using htaccess. Thus far I have -
To enforce SSL
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]
And To remove index.php
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]
it seems to work for the most part but when i enter something like - "example.com/index.php", i get an error. It is redirected to
https://www.example.com/https://www.example.com/
I know why its happening but can't figure out a rule to combine everything I need and make it work.
Going to take a guess, you're missing the L flag in the first rule:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Without the L flag, the request gets marked as "redirect" but continues on to have the other rules processed. And by the result you're seeing, it looks like the 2 rules are interferring with each other.
But is there any way to reduce the redirects when I type example.com/index.php
Not sure if this will work, because I can't test it, but maybe something like this?
RewriteCond %{THE_REQUEST} ^.*/index.php [OR]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^(.*?)(index\.php)?$ https://www.example.com/$1 [R=301,L]
Related
Could there be a better way of doing this I wonder. It is doing the following:
A) routing mydomain.uk to www.mydomain.uk
B) routing anything non http to https://www.mydomain.uk
I dont use linux based servers much so im sure there is a better way of doing this. End result should be any base url should end up at https://www.mydomain.uk
Heres the code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.uk\/" [R=301,L]
These 2 rules can be easily combined into one as this one:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.mydomain.uk%{REQUEST_URI} [L,R=301,NE]
You can use OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule (.*) https://www.mydomain.uk%{REQUEST_URI} [R,L]
The rule RewriteCond %{HTTP_HOST} ^mydomain.uk$ can also be more restrictive and catch any host which is not www.mydomain.uk.
How do I force SSL for a particular domain currently I have
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This is working, however I have several add on domains on my hosting and when I try to access the other add-on domains, the add-on domains are also forced to use SSL.
I tried this:
RewriteCond %{HTTP_HOST} ^exampledomain\.org$ [OR]
RewriteCond %{HTTP_HOST} ^www\.exampledomain\.org$
RewriteRule ^/?$ "https\:\/\/www\.examplemydomain\.org\/" [R=301,L]
But it is giving me an infinite loop.
This should work:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} exampledomain\.org$ [NC]
RewriteRule ^ https://www.examplemydomain.org%{REQUEST_URI} [R=301,L,NE]
It looks like you're doing 2 different things here. You're adding a www when one is missing, and you're forcing SSL when it isn't used. So there's 2 different conditions and either one being true should force a redirect. That means you want to use the [OR] flag, but the way you were using it breaks if the request is already SSL. Try:
RewriteCond %{HTTP_HOST} ^exampledomain\.org$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.examplemydomain.org/$1 [R=301,L]
try:
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.){0,1}exampledomain\.org$
RewriteRule ^/?$ "https\:\/\/www\.examplemydomain\.org\/" [R=301,L]
I have 4 domain which I want 3 of them redirect to last one. I already used some htaccess rules and they are working great,
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.com$ [NC]
RewriteRule ^(.*) http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.first.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.second.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [L,R=301]
the only problem is when I enter "www.first.com/about" it does not shows "www.forth.com/about" just shows the same thing . all the domain are forwarded to forth.com and they do not have hosting so I cannot put htaccess file for those other domains , please guide. would be appreciated.
This is most likely what is happening, at least on my test cases this fix handles it.
Instead of using [L,R=301] reverse the order and do [R=301,L]. The L means Last and it ends cycle on evaluating what to do at that point. I've always used [R=301,L] as are all the examples that I could find anywhere. Potentially your apache may be hitting that L and immediately pass back the new string to the URL parser and isn't sending it as a 301 Redirect as it didn't get that far.
Also on the first.com snippit above you left out the $ on the ^(.*) - only for that line though, not sure if this was a typo or if it's missing in your actual .htaccess file.
Otherwise, you could tidy it up a bit by using:
RewriteCond %{HTTP_HOST} ^((www.)?)first.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)second.com [NC,OR]
RewriteCond %{HTTP_HOST} ^((www.)?)third.com [NC]
RewriteRule ^(.*)$ http://www.forth.com/$1 [R=301,L]
I'm trying to run my entire site through https and force www.
I've seen a number of solutions that provide forcing either www or https, and even a few combined, but I can't seem to get any to work. I usually find myself in a redirect loop.
The closest I've got is the following, but it's not close enough yet:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
I need https://www.example.com/
http://example.com SUCCESS
https://example.com SUCCESS
http://www.example.com FAIL
https://www.example.com SUCCESS, although there's no actual redirection.
Thanks
Update
The following code successfully performs the redirection I require:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Forcing https and www is done by a combination of other provided answers.
This seems to work:
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{ENV:HTTPS} on [NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Try this condition instead, it should force the site into ssl for your domain
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Try adding an [OR] flag to your conditions. You really want either no www or no https:
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC,OR]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
I'm trying to make a general rewrite rule to redirect all domain.com requests to www.domain.com.
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+])$ [NC]
RewriteRule ^(.*)$ http://www.{HTTP_HOST}/$1 [R=301,L]
The problem is that this rewrite rule doesn't match anything. How can I change it? Thanks
I guess the problem is that there’s an additional ] in your RewriteCond’s pattern and that there is a % missing when referencing HTTP_HOST in RewriteRule’s substitution. So try this:
RewriteCond %{HTTP_HOST} ^([0-9a-z-]+)\.([0-9a-z-]+)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
And to cover Cags’ concerns a litte bit, you can also try this rule:
RewriteCond %{HTTP_HOST} ^[^./]+\.[^./]+$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^./]+)\.[^./]+\.[^./]+$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]