dynamic vhost configuration based on a substring of the URI - apache

I'm looking to perform a simple redirection to a root directory based on a substring of the URI. For example:
app1_{substring1}.mydomain.com will redirect to C:/wamp/www/substring1
app1_{substring2}.mydomain.com will redirect to C:/wamp/www/substring2
ServerAlias allows only wildcard...
Any ideas to implement that ?
Apache version: 2.2.22 (WAMP version)

Try this:
ServerAlias *.mydomain.com
DocumentRoot C:/wamp/www
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{HTTP_HOST} ^app1_([a-z0-9A-Z]+)\.mydomain\.com [NC]
RewriteRule ^ /%1%{REQUEST_URI} [QSA,L]

Related

magento site redirecting to http://www.hpslightshop.com/

Today I'm trying to configure the sub-domain in Apache web-server(Centos) using virtual-host.Sub domain pointing is working fine.
error
magneto site redirecting to different site (http://www.xxxx.com/)
Note :-
1.IN core_config_data table.I have changed the data to http://test.xxxx.com/
2.cleared /var/cache and /var/session folders in your magento root
<VirtualHost *.80>
DocumentRoot /var/www/html/
ServerName test.xxxx.com
ErrorLog /var/log/xxxx_error_log
CustomLog /var/log/xxx_access_log
</VirtualHost>
Add the below code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule>
if you run
select * from core_config_data where path like '%base_url%' is changed or not

mod_rewrite in httpd.conf not working (but does in .htaccess)

I have a very similar setup on 2 different servers (the working one is a Plesk install on Centos 6, the other is pretty plain LAMP setup on Centos 6) and im having an issue with mod_rewrite on the plain LAMP server.
I have compared the httpd.conf (and associated includes) on both servers and both are (for all intents and purposes) the same.
On the plesk server i have the following in my httpd.conf file which redirects http://stats.domain.com to http://www.domain.com/webstat/ and this works a treat;
<VirtualHost *:80>
ServerName stats
ServerAlias stats.*
UseCanonicalName Off
RewriteEngine On
RewriteCond %{HTTP_HOST} stats\.(.*) [NC]
RewriteRule ^.*$ http://www.%1/webstat/ [L,R=301]
</VirtualHost>
On the LAMP server i have the following in my httpd.conf file which is supposed to redirect http://stats.domain.com to http://www.domain.com/awstats/awstats.pl?config=domain.com
<VirtualHost *:80>
ServerName stats
ServerAlias stats.*
UseCanonicalName Off
RewriteEngine On
RewriteCond %{HTTP_HOST} stats\.(.*) [NC]
RewriteRule ^$ http://www.%1/awstats/awstats.pl?config=%1 [L,R=301]
</VirtualHost>
This is not working when in the httpd.conf file though. If i add stats.domain.com as virtualhost with full documentroot, etc and then add the same rewrite to .htaccess within that documentroot the redirect works fine.
If i enable rewrite logging then i can see the .htaccess version being worked through, but nothing from the httpd.conf. I cannot for the life of me work out what is wrong/missing :(
The rewrite rule for your LAMP server is just putting in domain.com.
RewriteCond %{HTTP_HOST} stats\.(.*) [NC]
RewriteRule ^$ http://%1/awstats/awstats.pl?config=%1 [L,R=301]
Your comment above says it should be www, more like the first rule.
RewriteRule ^.*$ http://www.%1/awstats/awstats.pl?config=%1 [L,R=301]

Apache: Don't apply RewriteRule if requested host is a IP address

How doesn't Apache apply an RewriteRule if the requests isn't the normal domain name but instead the server IP?
Current rules (to make domain just accessible without the www):
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
I tried already to add RewriteCond %{HTTP_HOST} !^123.321.123.321$ [OR] but it seemed not to work.
If you have access to the server config file, this works for me (setting it in httpd.conf):
<Location />
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^123.123.123.123$
RewriteRule (.*) http://www.mainsite.example.com/ [L]
</Location>
If I visit the site by IP address, it isn't rewritten. If I use (eg.) http://mainsite.example.com, the address is rewritten (to include the www.). The [L] (when used in httpd.conf) stops rewrite processing there (instead of now trying to rewrite the new address).
In there you can also have
ServerName www.mainsite.example.com
ServerAlias mainsite.example.com
ServerAlias www.mainsite
ServerAlias mainsite
ServerAlias 123.123.123.123
so that people can use any of those addresses (the 2nd and 3rd aliases let internal users omit the domain), instead of rewriting to force or add www.

Dynamicly set document root for subdomains using regex

I've setup subdomain wildcard to accept anything subdomian,the main problem is that i use DirectAdmin and i am not able to point subdomain to specific path using DA API
i want to point any subdomian to a folder named as subdomian which is exist in subdirectory
for example sub.domian.com should point to /public_html/subdomains/sub
something like this
<VirtualHost *:80>
VirtualDocumentRoot /public_html/subdomains/%1
</VirtualHost>
this would be my ideal if i set subdomains which content a word like ".x" to point in that path
maybe like
<VirtualHost *.x:80>
VirtualDocumentRoot /public_html/subdomains/%1
</VirtualHost>
sorry if question sounds stupid
looks like this is what you need:
<VirtualHost *:8080>
ServerName localhost
ServerAlias *.localhost
#the %1 is the asterisk value! No regexp supported!
VirtualDocumentRoot /home/%1/project
VirtualScriptAlias /home/%1/project
<Directory /home/*/project/>
#...
</Directory>
</VirtualHost>
This can be done totally using htaccess .
You have to enable wildcard subdomain name *.domain.com and point it to domain.com
Then you can use the following types of rules :
RewriteCond %{HTTP_HOST} !www.domain.com [NC]
RewriteCond %{HTTP_HOST} ^([a-z0-9-]+).domain.com [NC]
RewriteRule ^(.*/) /%1/index.php [PT,L]
You can modify RewriteRule as files for subdomains are stored .
found the answer
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !www\.domain\.com$[NC]
RewriteCond %{HTTP_HOST} ^(.*?)\.domain\.com$
RewriteCond %{REQUEST_URI} !folder/(.*?)/
RewriteRule ^(.*) /folder/%1%{REQUEST_URI} [L]
</IfModule>

virtual hosts map to directory structure

I have read the mass virtual host help on apache but as a relative newbie I am left a little confused. I know my problem must be acheiveable as it is a pretty basic problem but i am lost...
Basically I have lots of virtual hosts pointing to my server and the file structure that they point to for their DocumentRoot is consistent... e.g..
www.mydomain.com -> /home/blah/vhosts/mydomain.com/www/public
abc.mydomain.com -> /home/blah/vhosts/mydomain.com/abc/public
www.another.co.uk -> /home/blah/vhosts/another.co.uk/www/public
def.another.co.uk -> /home/blah/vhosts/another.co.uk/def/public
If possible, I also need to redirect non-www.* to www.* but taking into account the possibility of a subdomain, so that:
mydomain.com is redirected to www.mydomain.com
abc.mydomain.com is NOT redirected to www.mydomain.com as it is
handled with the definition above (due to directory structure)
Is this at all possible?
Here what I do for all my vhosts: I'm using Apache writemap.
Create a new "partner.txt" file with partners like this:
0 www
1 partner1
2 partner2
3 partner1
Then compile it, and add use it into your rewriterules to find out whether the prefix is a partner or not, like this:
<VirtualHost *>
ServerAdmin webmaster#mydomain.fr
DocumentRoot "/web/htdocs/olivier/mydomain.fr/dev/website"
ServerName mydomain.fr
ServerAlias *.mydomain.fr
ErrorLog "/web/logs/mydomain.error.log"
CustomLog "|/opt/httpd/bin/rotatelogs /web/logs/mydomain.fr/access_log.%Y-%m-%d-%H_%M_%S.log 5M" combined
ErrorDocument 404 /404.php
RewriteEngine On
# trying to hack = redirect:
RewriteRule (.*)setup.php http://disneyland.fr/ [NC,R,L]
RewriteRule (.*)admin(.*) http://disneyland.fr/ [NC,R,L]
# if your host doesn't begin with "www" add it and redirect:
RewriteCond %{HTTP_HOST} ^mydomain\.(fr|com|net|org|eu) [NC]
RewriteRule (.*) http://www.mydomain.%1$1 [QSA,R=301,L]
RewriteMap partners \
dbm:/web/htdocs/olivier/mydomain.fr/rewriterules/partners.map
# test if known partner:
RewriteCond %{HTTP_HOST} (([a-zA-Z0-9\-]+)\.)mydomain.com$
RewriteRule (.*) - [QSA,E=PARTNER:${templates:%1|notfound}]
# if partner not found or empty, 404:
RewriteCond %{ENV:PARTNER} ^$ [OR]
RewriteCond %{ENV:PARTNER} notfound
RewriteRule .* - [R=404,L]
</VirtualHost>