.htaccess add www AND ensure https:// - apache

Been wrestling with this for quite a while with no success. What I'm looking for is an .htaccess rule to ensure all requests:
always start with "www"
are always secure (https)
Of all the rules I tried, perhaps this was the closest:
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301]
This set of rules works great for:
http://www.example.com > https://www.example.com
http://example.com > https://www.example.com
Doesn't work for:
http://www.example.com/index.cfm?myquerystring
http://example.com/index.cfm?myquerystring
UPDATE:
The actual problem in my case is that for certain file extensions (.cfm,.cfml,.cfc) the request was being "handed off" to the application server before my rewrite rules were able to take affect. Thanks to all who responded.

Try this rule:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80 [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

This should do the job:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
Please, note that the .htaccess should be located in the website main folder.

Related

www and non-www http requests to https://www with full path

I created redirect from non-www and www http request to https and www request that was quite easy. I used following rules in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
But now the problem is when someone enters old non-secure url like domain and project-about.html at the end .htaccess redirect to https but adds request at the end again so the request after the domain look like:
/project-about.htmlproject-about.html
How can I fix it? Thanks
Change it to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
This method removes the need for an extra RewriteRule also, helping to speed up your website.
Make sure you clear your cache before testing this.

Redirecting all but one pattern of file to new site

If I have http://example.com and I want to redirect all traffic except orange.1.php, orange.2.php, orange.32.5.php, etc to http://newexample.com, how would I write the .htaccess? Just to be clear, http://example.com/blah should redirect to http://newexample.com, not to http://newexample.com/blah. I've googled for an hour trying different things, and none seem to work.
I thought this should work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^orange\.[0-9]+.*$
RewriteRule ^/?.*$ "http:\/\/newexample\.com" [R=301,L]
Thanks!
REQUEST_URI always starts with a forward slash.
You can use this refactored shorter rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule !^orange\.\d+\.php$ http://newexample.com/? [R=301,L,NC]

htaccess redirect to https://www url

I have to redirect my site on https://www.example.com using htaccess.
I have used many solutions but when I remove "s" from https my site runs on http & when I only run the www.example.com then it will run on http it works only with the "example.com".
Solution which I have used is mentioned below
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
Does any one have a magic solution.
Use:
# http(s)://domain -> https://www.domain
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.domain.com%{REQUEST_URI} [NE,R=301,L]

Redirect http://www.domain.com to https://www.domain.com using .htaccess

I want to redirect my domain from http to https:
http://www.domain.com --> https://www.domain.com
Is it possible? I've been searching around the internet but I only found:
http://domain.com --> https://www.domain.com
The question is, how about peoples arriving directly on http://www.domain.com? Aren't they be served with non-https url? Same as the vice versa. I just want a simple redirection from the HTTP to HTTPS. Is it possible?
Thank you
I just want a simple redirection from the HTTP to HTTPS
Try this simple rule as first rule on your .htaccess:
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
It will work for ALL versions of php and will force SSL and www to all naked domains and links:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
We DO need the NE
www. is optional in 4th line
"off" is required to be.
Use the following code for force www and SSL usage:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^tatwerat\.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
</IfModule>

mod_rewrite - add www

I'm trying to force www for my site address with .htaccess:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.coml/$1 [R=301,L]
If I open mysite.com this working ok - it adds www. It becomes http://www.mysite.com/index.php.
But if I open mysite.com/subpage I redirected to http://www.mysite.com/index.php again, instead of http://www.mysite.com/subpage.
But if I open http://www.mysite.com/subpage I don't get redirect (which is the expected behaviour).
How can I fix this? I would like the redirect all requests to my site to the www subdomain.
From the superb HTML5 Boilerplate .htaccess:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
The problem is right here:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
^ specifies the beginning of the string while $ specifies the end.
In order to make it work you need to have a wildcard select after the .com or .+.
to reiterate what Alex said:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Actually, I have to disagree with Mr. Szanto.
It would appear the problem was not with the:
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
Through some additional research (via Google) I found that this worked great for me:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
Mr. Szanto's answer, while well formatted, seems to redirect all requests missing the 'www' to be redirected with the 'www' attached. This was not a desirable outcome with most subdomains. The above code only redirects if there is no 'www' AND no subdomain before the TL domain name.
Hope this helps others.