.htaccess HTTP to HTTPS AND non-www to www - apache

I currently have some rules which work for directing http://domain.com to https://www.domain.com, but it doesn't work (I guess it's not matching?) for http://www.domain.com, which should redirect to https://www.domain.com. Could someone modify the below to do this? I've tried quite a few things but haven't been successful, this is my first time with .htaccess rewrite rules.
TL;DR, I need to redirect to both WWW and HTTPS
RewriteEngine On
RewriteRule .? - [E=PROTO:http]
RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ %{ENV:PROTO}://www.%{HTTP_HOST}/$1 [R=301,L]
EDIT: I've found the following code, which somewhat works - It redirects both to https://www.domain.com, but it gives a "Too many redirects" error.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} (www\.)?(.+)$ [NC]
RewriteRule ^ https://www\.%2%{REQUEST_URI} [L,R=301]

Have you tried something like this?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.domain.com/$1 [L,R=301]
It would be better to modify this in the VirtualHost for the server.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
Redirect permanent / https://www.domain.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName www.domain.com
DocumentRoot /www/
SSLEngine On
# etc...
</VirtualHost>

This is the rule I'm using:
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
You could include RewriteCond %{HTTPS} !=on as the first condition to force remove https if that's neccesary...
Hope that helps...

Related

.htaccess redirect olddomain/folder to newdomain/otherfolder

I use this in my httpd.config and it works:
<VirtualHost *:80>
ServerName olddomain.com
ServerAlias www.olddomain.com
Redirect permanent /FolderName/Filename_with_underscores.html http://newdomain.com/some-folder-with-dashes/?lang=fr
Redirect permanent /FolderName/Other_filename.html http://newdomain.com/some-other-folder/?lang=fr
Redirect permanent / http://newdomain.com/
</Virtualhost>
Now I'd like to put this in the .htaccess file in the root of the website.
I have tried several things but it keeps failing.
Does anyone know how to translate this httpd redirect to .htaccess?
There are capitals, dashes and underscores in the url's...
The code below works (but it's only half the story: it redirects all (www.)olddomain.com to the desired folder at newdomain.com):
RewriteCond %{HTTP_HOST} ^olddomain\.com\$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/?$ "http://newdomain.com/some-folder-with-dashes/?lang=fr" [R=301,L]
Thnx
You can use in your root .htaccess:
RewriteEngine on
# All rules only for olddomain.com
RewriteCond %{HTTP_HOST} !^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ - [L]
RewriteRule ^FolderName/Filename_with_underscores\.html$ http://newdomain.com/some-folder-with-dashes/?lang=fr [NC,R=301,L]
RewriteRule ^FolderName/Other_filename\.html$ http://newdomain.com/some-other-folder/?lang=fr [NC,R=301,L]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]

How to put "and" and "or" condition in RewriteCond?

This is my current VirtualHost node :
<VirtualHost *:80>
ServerName abc.com
ServerAlias abc.com www.abc.com
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/ [NC,R,L]
</VirtualHost>
It conveys that every request:80 coming to this server will redirect to HTTPS. Now i want to add or and and condition to VirtualHost node. So, that it will work as follow:
If request:80 coming from xyz.com then it will not redirect to HTTPS
If request:80 coming from abc.com then it will redirect to HTTPS
How should i do it ?
You can say:
RewriteCond %{HTTP_HOST} ^(www\.)?abc\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^/?(.*)$ https://%{HTTP_HOST}/$1 [NE,R,L]
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$ [NC]
RewriteCond %{HTTPS} on
RewriteRule ^/?(.*)$ http://%{HTTP_HOST}/$1 [NE,R,L]

www url rewrite not working (Read All Other Threads)

i want to rewrite
example.com to www.example.com
My Code in apache2.conf (Main config file of apache in Linux/ubuntu 32bit ):
try 1:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
Restart Apache Code Doesn't Work !
try 2:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
Restart Apache Code Doesn't Work !
try 3:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
Restart Apache Code Doesn't Work !
Loaded Modules in apache displays:
rewrite_module (shared)
How can i solve this ?
And is there any other mod i have to enable(I don't think so).
I have to put this code in some other file ?
It is neccessary/must use .htaccess for url rewriting ?
Your rules are fine.
Make sure the rules are in the virtualhost of the "example.com" domain. If you have no "example.com" vhost, then place the rules in the first vhost container. S
<VirtualHost *:80>
ServerName example.com
....
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
....
</VirtualHost>
Try putting the rules in a directory container:
<Directory "/var/www/path/of/your/document_root/">
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</Directory>

mod_rewrite condition and rule

I've searched for this and Im probably just sitting to close but I cant seem to get this rewrite to work. I have a file on a directory that I want to redirect to a vhost domain on the same server. Below is my condition, rule, and vhost
RewriteCond %{HTTP_HOST} ^/main$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R,l]
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /data/live/main
</VirtualHost>
If you want to redirect the olddomain folder main to the newdomain then you can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^main/(.*)$ http://newdomain.com/$1 [R=301,L]
If you want to redirect all the content of the olddomain to the newdomain then you can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
And if you want to remove the www from your newdomain:
RewriteCond %{HTTP_HOST} ^www\.newdomain\.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
Or you can do it directly on the virtualhost like this:
<VirtualHost *:80>
ServerName newdomain.com
Redirect permanent / http://www.newdomain.com
DocumentRoot /data/live/main
</VirtualHost>

Multi-condition rewrite rules

I have a site with areas that require forced SSL mode and then forced to non-SSL for the rest.
I have started with the following rules:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/tourism/bookings/(.*) https://%{SERVER_NAME}/tourism/bookings/$1 [R,L]
RewriteRule ^/?bookings/(.*) https://%{SERVER_NAME}/bookings/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/bookings
RewriteCond %{REQUEST_URI} !^/tourism/bookings
RewriteRule (.*) http://%{SERVER_NAME}$1 [L,R,QSA]
</VirtualHost>
Now, the above works - but the SSL mode obviously interprets /includes/* to force it to non-SSL... is there an adjustment to the above to allow me to force ALL content except the two above (but any dependencies, like JS / CSS includes to follow the current protocol)?
Thanks in advance
If you want to exclude other things besides /bookings and /tourism/bookings then simply exclude them in conditions.
Also, you don't need the RewriteCond %{HTTPS} !=on condition since that virtual host is always going to be non-HTTPS:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule ^/tourism/bookings/(.*) https://%{SERVER_NAME}/tourism/bookings/$1 [R,L]
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule ^/?bookings/(.*) https://%{SERVER_NAME}/bookings/$1 [R,L]
</VirtualHost>
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/bookings
RewriteCond %{REQUEST_URI} !^/tourism/bookings
RewriteCond %{REQUEST_URI} !\.(css|js)$ [NC]
RewriteRule (.*) http://%{SERVER_NAME}$1 [L,R,QSA]
</VirtualHost>