Currently the website being hosted uses the following structure of test.mysite.com/{site} which then serves /var/www/test/{site}
I've been trying to create a virtualhost that can match several subdomains and serve the folders under /var/www/test/. The urls would look like {site}.test.mysite.com
I've tried several approaches, one using virtual document root:
<VirtualHost *:80>
ServerName test.mysite.com
ServerAlias *.test.mysite.com
VirtualDocumentRoot /var/www/test/%1/
</VirtualHost>
This works well except that the folders under /var/www/test/ are in uppercase and I cant seem to get the %1 to uppercase.
A different approach was using mod rewrite as specified on the apache site
<VirtualHost *:80>
ServerName test.mysite.com
ServerAlias *.test.mysite.com
RewriteEngine on
RewriteMap upper int:toupper
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9-]+\.test\.mysite\.com$
RewriteRule ^([a-z0-9]+)\.test\.mysite\.com/$ /var/www/test/${upper:$1}/
</VirtualHost>
However it never seems to actually rewrite and just keeps serving the default apache document root.
I'd prefer not to have to change the name of all the folders to lowercase due to other dependencies. Are there any other options for doing this?
Your rule is wrong: RewriteRule, here, only apply to the full path of the URL which does not include the host. Try:
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+)\.test\.mysite\.com$
RewriteRule .* /var/www/test/${upper:%1}$0 [L]
Related
I have read a number of formulas for redirecting example.com to www.example.com and this appears to apply to .htaccess. However, I am confused about how this might work.
I want to assume that I have no access to the Apache vhosts configuration, and that I need to do it with .htaccess.
Suppose the configuration contains something like this:
<VirtualHost *:80>
ServerName www.example.com:80
ServerAlias www.example.com
VirtualDocumentRoot /whatever/example.com/www
</VirtualHost>
One such formula is something like this"
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ www\.%{HTTP_HOST}/$1 [R=301,L]
However, I don’t see how one root directory can respond to both requests.
The question is: Is it even possible to redirect example.com to www.example.com using .htaccess only, without tweaking the Virtual Host file?
The way I see it, the original request
I run my own server, and I can do anything I like. However, I am asking this because many of my clients and students have no access to tehe configuration, and can only fiddle with .htaccess.
I need a rule to internally rewrite several domains, with and without www:
www.a.com --> /m/n/o/
b.c.org --> /x/y/z/
The setup is Apache running locally on Windows (XAMPP). I've got the hosts file set up so all the domains point to localhost. I'd like every page to get redirected, i.e. I want to point each domain to it's own different root directory and have it work normally from there. e.g.
/ <-- Top level folder, everything is under here.
/root/of/domain/A/ <-- www.a.com
/root/of/domain/C/ <-- b.c.org
You have two choices.
(1) The one you asked (with mod_rewrite)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?a\.com$ [NC]
RewriteRule ^/(.*)$ /root/of/domain/A/$1 [L]
RewriteCond %{HTTP_HOST} ^b\.c\.org$ [NC]
RewriteRule ^/(.*)$ /root/of/domain/C/$1 [L]
</IfModule>
Note: don't forget to replace example values by real ones. Also, make sure mod_rewrite is enabled.
(2) the cleanest way: configure virtualhosts directly (without mod_rewrite)
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot "X:/path/to/root/of/domain/A/"
ServerName a.com
ServerAlias www.a.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "X:/path/to/root/of/domain/C/"
ServerName b.c.org
</VirtualHost>
I have a domain, lets say mydomain.com.
I want every subdomain of this to load the contents of the directory with the same name... For example, if someone writes
http://sub.mydomain.com/index.php
I want to show him the contents of
http://sub.mydomain.com/sub/index.php
Still, I want it to show in the addressbar the http://sub.mydomain.com/index.php
The apache vhost is like
<VirtualHost *:80>
ServerName *.mydomain.com
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
so in the filesystem, the files for the example above would be under the directory
/var/www/vhosts/mydomain.com/subdomains/subs/www/sub
I tried many of the proposed solutions here, but most of them were redirects to some other domain/subdomain or end up in a redirect loop :(
tia
You need to add some rewrite rules that will examine the sub domain and then rewrite to the relevant path:
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias *.mydomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} (.*)\.mydomain\.com
RewriteRule ^/(.*)$ /%1/$1
DocumentRoot /var/www/vhosts/mydomain.com/subdomains/subs/www
... more stuff ...
</VirtualHost>
What this will do is capture anything preceding ".mydomain.com" then rewrite it into the URL as %1, $1 will be the requested resource such as index.html
Be aware this might trip you up if www.mydomain.com is a valid domain for your site!
Try this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.[^.]+\.[^.]+$ [NC]
RewriteRule !^sub(|/$) /sub%{REQUEST_URI} [L,NC]
I'm really new to apache mod_rewrite module. I have a page called http://abc in my company intranet. I want users to be redirected to http://abc.somecompanyname.com whenever they type http://abc to the URL bar. Could someone please provide and example or point me in the right direction.
I figure this should be quite an easy question to answer. Thanks everyone for you inputs.
-Mark
Quote from Apache 2.4 documentation:
The very best way to solve this doesn't involve mod_rewrite at all, but rather uses the Redirect directive placed in a virtual host for the non-canonical hostname(s).
<VirtualHost *:80>
ServerName undesired.example.com
ServerAlias example.com notthis.example.com
Redirect / http://www.example.com/
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
</VirtualHost>
This does require another virtual host, but there's no shortage of those. The solution works very well for me - and I like how redirection of 'unwanted' hosts and configuration of the canonical host are separated.
You could accomplish that with a VirtualHost definition as simple as this, on the server handling requests for abc:
<VirtualHost *:80>
ServerName abc
RewriteEngine on
RewriteRule ^/(.*)$ http://abc.somecompanyname.com/$1 [R,L]
</VirtualHost>
I found the advise in the Apache2 URL Rewriting Guide worked better.
I ended up with:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^foo\.bar\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://foo.bar.com/$1 [L,R]
The "RewriteEngine on" line wasn't included in the Apache2 example. Maybe it's usually on by default but in my case I needed to add it.
I 2 domains with and without the www prefix. When a user visits any of these domains, I want it to automatically reroute to a chosen 1 of them.
For example:
domain.com
www.domain.com
domain.co.uk
www.domain.co.uk
When a user visits www.domain.com, domain.co.uk or www.domain.co.uk, it will rewrite to domain.com
So far, I have my apache2 virtual host block setup like this:
<VirtualHost *:80>
ProxyPass / http://localhost:3060/
ProxyPassReverse / http://localhost:3060/
ServerName domain.com
ServerAlias www.domain.com
ServerAlias domain.co.uk
ServerAlias www.domain.co.uk
</VirtualHost>
But this doesn't do the rewriting/rerouting. I need to also make sure that it takes into account any paths. For example, www.domain.co.uk/test would change to domain.com/test
Any ideas how I can do this in the virtual host block? I'm assuming I would split the 3 domains to be rewritten into a separate block and treat them there, but really not sure how to accomplish all the rules I need.
Per your comment, you want to redirect the three aliases to the main domain, and you've stated that you would like to do this within the virtual host configuration.
<VirtualHost *:80>
...
RewriteEngine on
# If using one of the aliases ...
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk
# ... redirect to the main domain
RewriteRule ^(.*)$ http://domain.com/$1 [R=302,L]
</VirtualHost>
You can also add the Rewrite* directives in your domain's .htaccess file.
To make the redirect permanent, change 302 to 301 - this basically instructs browsers and search engines to cache the redirect.