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>
Related
I have a virtual host for www.domain.com which redirect all to https
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com *.domain.com
RewriteEngine on
RewriteCond %{SERVER_NAME} =*.domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
This will redirect all request from
domain.com to https://domain.com
www.domain.com to https://www.domain.com
*.domain.com to https://*.domain.com
but i want to enforce www on domain.com.
I tried:
RewriteEngine on
RewriteCond %{SERVER_NAME} =*.domain.com [OR]
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
but this is not working..
can someone help em to achieve this without having to create another VirtualHost just for domain.com
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias domain.com *.domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} =domain.com [OR]
RewriteCond %{HTTP_HOST} =www.domain.com
RewriteRule ^/(.*)$ https://www.domain.com/$1
RewriteCond %{HTTP_HOST} !=domain.com
RewriteCond %{HTTP_HOST} !=www.domain.com
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1
</VirtualHost>
The first Rewrite Rule will redirect all requests from both http://domain.com and http://www.domain.com to https://www.domain.com
The second Rewrite Rule will redirect all requests from http://*.domain.com to https://*.domain.com
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]
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]
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>
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...