Subdomain to subfolder in Apache - 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.

Related

Fallback - redirect to a new domain using apache vhost configuration

This is a followup question on https://stackoverflow.com/posts/47547418
I wanted my requests from
somedomain.com/loadproduct?product=dell-inspiron-15
to be selectively redirected to
someotherdomain.com/dell-inspiron-15
but at the same time I want to make sure if something goes wrong with the new domain, users are still able to use old domain by adding /old in the url.
For example if users uses
somedomain.com/old/loadproduct?product=dell-inspiron-15
then they should not be redirected to someotherdomain but should be served through a valid url somedomain.com/loadproduct?product=dell-inspiron-15
but if they use
somedomain.com/loadproduct?product=dell-inspiron-15
they should be redirected.
Currently my vhost configuration looks like below. It redirects to someotherdomain for selected products but there is no fallback configuration.
Listen 12567
NameVirtualHost *:12567
<VirtualHost *:12567>
ServerName somedomain.com
ProxyPreserveHost On
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-15) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-16) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-17) [NC]
RewriteRule ^/?loadproduct$ http://someotherdomain.com/%1? [R=301,L,NC]
</VirtualHost>
Any leads here is really appreciated.
Modify your RewriteCond conditions and add one where it says not to accept /old/ in the path (URI). So:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/old/.*$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-15) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-16) [NC,OR]
RewriteCond %{QUERY_STRING} (?:^|&)product=(Dell-Inspiron-17) [NC]
RewriteRule ^/?loadproduct$ http://someotherdomain.com/%1? [R=301,L,NC]
In the first line, no need to specify [AND], as it is implicit. The ! character negates the match.
Disclaimer: I have not tested this on a real Apache, but I am pretty sure it is ok.

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]

.htaccess Rewrite Engine setting exemption to rule shared SSL

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]

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]