Multi-condition rewrite rules - apache

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>

Related

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>

.htaccess HTTP to HTTPS AND non-www to www

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...

Send one subdomain to subdirectory, everything else to file with parameter

Hoo-boy I've been struggling with this one all day.
The redirect from the subdomains to the script files with subdomain passed in as a parameter work fine.
When one particular subdomain is actually a real folder (phpmyadmin), I can't seem to make it work, at least not with the previously mentioned rule in place.
Here's my stuff:
<VirtualHost *.domain.com:443>
ServerName *.domain.com
ServerAlias *.domain.com
DocumentRoot /home/domain/web
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(phpmyadmin)\.domain\.com
RewriteCond %{REQUEST_URI} !^phpmyadmin
RewriteRule ^(.*)$ /phpmyadmin [L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteRule ^\/script2$ /public/script2.php?param1=%1 [L]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com
RewriteRule ^(.*) /public/script1.php?param1=%1 [L]
<Directory /home/domain/web>
Options -Indexes IncludesNOEXEC FollowSymLinks -MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
SSLEngine on
SSLCertificateFile /home/domain/crt
SSLCertificateKeyFile /home/domain/key
</VirtualHost>
Any ideas?
2 problems with:
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(phpmyadmin)\.domain\.com
RewriteCond %{REQUEST_URI} !^phpmyadmin
RewriteRule ^(.*)$ /phpmyadmin [L]
In your target redirect, you do not change the domain. By just using /phpmyadmin, you're not directing to www.domain.com/phpmyadmin, you're directing to phpmyadmin.domain.com/phpmyadmin, which is likely to cause an infinite redirect loop since it will keep matching the phpmyadmin subdomain each redirect. Use:
RewriteRule ^(.*)$ http://www.domain.com/phpmyadmin [L]
Unlike RewriteRule where the initial / is not used, RewriteCond %{REQUEST_URI} does need the leading slash like so:
RewriteCond %{REQUEST_URI} !^/phpmyadmin
Failing to do so means its not matching the directed /phpmyadmin, leading to another potential self-matching infinite redirect loop.
Also you don't need
RewriteCond %{HTTP_HOST} !^www. [NC]
since your url would never start with www and also start with phpmyadmin. Matching
RewriteCond %{HTTP_HOST} ^(phpmyadmin)\.domain\.com
automatically implies the NOT www match condition is also true