.htaccess Rewrite Engine setting exemption to rule shared SSL - apache

I currently have multiple domains pointing to the same web space ie domain.com domain.co.uk ect...
and i use .htaccess to force all requests to domain.com, which works great however i need to use a shared SSL provided by my host which points to my web space on the server, and i access it using the shared ssl domain. ( http://shared_ssl.com/domain.co.uk ).
The problem i have is .htaccess keep re-writing the domain to http://www.domain.com, when i try an access the shared ssl.
Is there a way to have an exception to the re-write rule?
here is my .htaccess file
RewriteEngine on
# Rewrite domain.org-> domain.com
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]

Don't know if I understand your problem exactly, but this is how would do it:
RewriteEngine on
# Rewrite domain.org-> domain.com
#RewriteCond %{HTTP_HOST} . # <- Not necessary
# (you can have the whole shared_ssl.com if you want, but not necessary)
RewriteCond %{HTTP_HOST} !^shared_ssl [NC]
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
# (and this is the preferable way to write it - I think)
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [QSA,R=301,L]

Related

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Mod rewrite to reidrect all traffic to different domain, except for set list of folders

We are gradually moving a clients site to a new system & domain name.
In the meantime we need both sites live - but sharing bits of the same domain.
I have now pointed the primary domain to the new server - www.newserver.org
On this server I need Apache to redirect any requests not in a list of allowable folders back to the old server - now living on: www.oldserver.org (preserving the path after the domain).
I also need the new server to be accessible without redirecting when accessed via it's IP address.
My best attempt at the apache configuration:
DocumentRoot /var/www
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/landing/
RewriteCond %{REQUEST_URI} !^/admin/
RewriteCond %{REQUEST_URI} !^/user/
RewriteCond %{REQUEST_URI} !^/sites/
RewriteCond %{REQUEST_URI} !^/scripts/
RewriteCond %{HTTP_HOST} ^www.newserver.org$
RewriteRule ^(.*) http://www.oldserver.org/$1 [NC,R=301,L]
Can anyone assist with the right RewriteCond & ordering?
At the moment all requests - even to the specified folders are redirecting.
---- also tried:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.newserver.org$
RewriteCond %{REQUEST_URI} !^(landing|admin|user|sites|scripts) [NC]
RewriteRule ^(.*) http://www.oldserver.org/$1 [NC,R=301,L]
suggested on another forum but this is still redirecting all requests - even to those folders
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.newserver.org$
RewriteRule ^(?!(landing|admin|user|sites|scripts)/)(\w+/.*)$ http://www.oldserver.org/$2 [NC,R=301,L]
You don't need to check for HTTP_HOST, unless both domains are served by the same system.
You are missing the leading slash, when checking for REQUEST_URI
RewriteCond %{REQUEST_URI} !^/(landing|admin|user|sites|scripts) [NC]

Subdomain to subfolder in Apache

I am looking for Apache configuration that would allow for the following:
Serve sub.domain.com as /%docRoot%/domain.com/sub, and
Would be able to do this for every hosted domain and any subdomain (ie. no per-domain vhost configuration)
I would be grateful for any solution, especially if there was no mod_rewrite involved (using mod_vhost_alias).
Note: There are some obvious solutions using mod_vhost_alias, but they either work for domain.com or sub.domain.com, none of them seems to cover both cases.
Have a nice day!
Point *.domain.com to your document root (/%docRoot%/). You'll need to do this in a vhost config. In that same vhost, add this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^/(.*)$ /domain.com/%1/$1 [L]
If you want to avoid pointing www.domain.com to /%docRoot%/domain.com/www, then add a condition to exclude it:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$ [NC]
RewriteRule ^/(.*)$ /domain.com/%1/$1 [L]
EDIT:
I assume I will still have to do this for every hosted domain (as the examples you've posted all refer to "domain.com"). Am I right?
Yes, the above will only do the routing for domain.com, if you want to do this for all arbitrary domain.com, you'll need to do something a bit more tricky:
RewriteEngine On
# here, %1 = subdomain name, and %2 = domain name
RewriteCond %{HTTP_HOST} ^([^\.]+)\.(.+)$ [NC]
# make sure the request doesn't already start with the domain name/subdomain name
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
# rewrite
RewriteRule ^/(.*)$ /%2/%1/$1 [L]
The tricky thing here is the %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1 match. It posits the condition: %{REQUEST_URI}:domain.com/sub and makes sure the %{REQUEST_URI} doesn't start with the domain.com/sub (back refrenced from previous match using %2/%1) using the \1 back reference.
With this, you setup your vhost to accept every domain (default vhost) and any subdomain/domain will get routed. Examples:
http://blah.bleh.org/file.txt goes to /%docRoot%/bleh.org/blah/file.txt
http://foo.bar.com/some/path/ goes to /%docRoot%/bar.com/foo/some/path/
http://sub2.sub1.d.com/index.html goes to /%docRoot%/sub1.d.com/sub2/index.html
EDIT2:
Yes, I would very much like to get domain.com routed to /%docRoot%/domain.com/
Try these:
RewriteCond %{HTTP_HOST} ^(.+?)\.([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%2/%1 !^/([^/]+/[^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%2/%1/$1 [L]
RewriteCond %{HTTP_HOST} ^([^\.]+\.[^\.]+)$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1
RewriteRule ^/?(.*)$ /%1/$1 [L]
Basically the same thing, except a few of the regex needs to be tweaked to separate what a domain.com is like vs sub.domain.com. If you want to redirect www.domain.com to domain.com, that needs to happen before these rules.

apache mod rewrite for subdomain

On my webserver, I have one website located at example.com/site (in /var/www/site).
I want it to be accessed through site.example.com, and I'm aware I need to use mod-rewrite to enable this.
Is there a concise snippet I can use to do this? I need to make it extensible so other sites can be accessed this way as well. Thanks in advance.
If this is a matter of one specific subdomain: site, then you should explicitly rewrite just that one subdomain:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ /site/$1 [L]
The second condition prevents an internal rewrite loop and making your URI look like /site/site/site/site/site/site/ etc...
This would go in your .htaccess file in your document root, /var/www/
If you really want to arbitrarily redirect any subdomain, you can try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]*)\.example\.com$ [NC]
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)[^:]*:\1 [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
Here, the second condition serves the same purpose, it tries to make sure the request URI doesn't start with the subdomain match from the host. See https://stackoverflow.com/a/11508430/851273 for a better explanation.
First you need to configure your server to accept any subdomain for your domain example.com and redirect it to your virtual host that as well has to accept any subdomain. After that, you can use the following rule to rewrite that subdomain internally to a folder with the same name:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$
RewriteRule ^ %1%{REQUEST_URI} [L]

Apache Rewrite: *FULL* non-www to www permanent redirect

I'm having difficulty constructing a non-www to www permanent redirect. Here are the conditions I need to meet...
1.) Multi-domain support (multiple website domains pointed at the same directory on a server with their own dedicated copy of a database). This means using %{HTTP_HOST} and NOT a static domain name.
2.) ALL possible pathes (or at least standards compliant ones) need to redirect; this means directories, HTTP queries, etc.
3.) I'm using shared hosting, so I have access to the .htaccess file only.
Here is what I'm working with right now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}
RewriteRule (.*) http://www\.%{HTTP_HOST}/$1 [R=301,L]
Details:
I'm testing with the browser cache disabled just in case.
I've removed other syntax during testing to see what replies work in their own right.
Here you go:
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It will also keep the URI scheme.
This here works for me (with my domainname)
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^%{HTTP_HOST}
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
The only difference I see is the matching pattern at the RewriteRule.