apache mod rewrite for subdomain - apache

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]

Related

Rewriting sub-domain leads to a HTTP redirect rather

What I'm trying to do here is rewrite my https://demo.example.com/ sub-domain to my https://example.com/ domain (in another folder), without changing the URL. However, the sub-domain keeps on redirecting me to the folder. I'm not great at HTACCESS and rewriting, but this should be an easy task to do.
This is my current HTACCESS file:
RewriteEngine On
RewriteRule ^license/?(.*)$ /sites/site-licenses/$1.php [L]
RewriteRule ^portfolio/?(.*)$ /sites/site-portfolios/$1.php [L]
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net
RewriteRule ^(.*)$ https://hamiltondev.net/sites/site-demos/$1 [L,NC,QSA]
The first two rewrite rules is for the main domain https://example.com/license/--- and https://example.com/portfolio/---.
When a full url is used as a target in a RewriteRule, it automatically becomes a redirect. What you need is either use an absolute path (if demo and main domain share the same document root folder) or use a proxy request.
both domains share document root folder
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net$ [NC]
RewriteRule ^(.*)$ /sites/site-demos/$1 [L,NC,QSA]
otherwise, proxy
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net$ [NC]
RewriteRule ^(.*)$ https://hamiltondev.net/sites/site-demos/$1 [P,NC,QSA]

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]

.htaccess redirect all subdomains to one directory, and preserve url

I have spend most of the afternoon googling this one and cant seem to get it to work at all (I am a bit rusty with htaccess!)
Basically I have a site, and every user registered has a subdomain (e.g. userA.example.com, userB.example.com)
I have been using a php script to register these subdomains, but now with over 500(!) subdomains, I am moving to a new server and thought I could possibly implement a new system.
I would basically like any subdomain appended to the domain to point to a single folder and keep the original url in the browser's address bar, so that I don't have to use server resources to register a new subdomain for every user!
I have already setup the wildcard DNS required for this.
I am using the following code to perform the redirect, but the address still changes:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.XXXX\.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.XXXX\.com$ [NC]
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ http://XXXX.com/frontend/ [L,NC]
This does redirect absolutely fine, but I cannot figure out a way to preserve the original URL with subdomain.
Thanks for your help
You need to first enable mod_proxy via your Apache config otherwise URL will change since you're changing domain name here.
Once mod_proxy is enabled try this rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.XXXX\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://XXXX.com/frontend/ [L,NC,P]
I think a simpler method of handling any complex redirection is to use PHP. In the server config file or an .htaccess file, use the FallbackResource directive to send all URL requests to a single PHP file. In that file, use $_SERVER['HTTP_HOST'] to get the hostname and $_SERVER['REQUEST_URI'] to get the original URI. Then you can use
header('Location: https://subdomain.example.com/');
to redirect to any calculated URL you wish.

.htaccess for domain change with subdomains - apache + plesk

We're using a plesk based system and just recently changed domain names. I'd like to redirect all requests coming in to the old domain to the new. There are many question asked in a similar fashion but mine is a bit different. I'd like to ensure that all subdomains get routed to the same subdoamin on the new domain. I set up a generic htaccess in the docroot but for some reason it is also applying to all subdomains.
RewriteCond %{HTTP_HOST} !newdomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
How can I make this more general so that subdomains also appropriately get routed? For bonus points, how can I route https requests to https and http to http.
I'd like to add that the rule transforms the first url to the second which is not desirable:
http://SUBDOMAIN.olddomain.com/somepath/somefile.php
http://newdomain.com/subdomains/SUBDOMAIN/httpdocs/somepath/somefile.php
The proper transform should create the following url:
http://SUBDOMAIN.newdomain.com/somepath/somefile.php
Lastly, this should work with wildcard subdomains.
Thanks in advanced!
Try:
# ignore "www."
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://%1.newdomain.com/$1 [L,R=301]
You'd want this before the rule that you already have.
And since you've edited your question, you want an additional internal rewrite for handling subdomains in general on your new site, this has nothing to do with the redirect. This is brand new functionality that is outside of the scope of a 301 redirect. You're going to need special rules to handle internal routing of subdomains:
RewriteCond %{REQUEST_URI} !^/subdomains/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.newdomain.com$ [NC]
RewriteRule ^(.*)$ /subdomains/%1/httpdocs/$1 [L]

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.