Dynamicly set document root for subdomains using regex - apache

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>

Related

Apache: Using VirtualHost redirect to URL with RewriteCond applied?

So I have a RewriteCond that turns:
domain.com/view.php?a=1
into:
domain.com/artist/name
I have a shorter domain: dmn.com that I would like to provide shortlinks for artists:
dmn.com/name
How can I get dmn.com/name to redirect to: domain.com/artist/name while keeping the shortlink in tact?
.htaccess:
#rewrite profile requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule artist/(.*)$ view.php?a=$1 [QSA,NC,L]
httpd.conf:
<VirtualHost *:80>
DocumentRoot /var/www/html/ // <-- ??? not sure what to put here
ServerName dmn.com
ServerAlias www.dmn.com
</VirtualHost>
Since the directory "/artist" doesn't actually exist (since it's being rewritten), I can't seem to put /var/www/html/artist in the DocumentRoot of the VirtualHost config without Apache spitting back:
Warning: DocumentRoot [/var/www/html/artist] does not exist
I think what you want is not an actual redirection, but another rewrite.
You can't do it with a separate VirtualHost. Well, you can if you point it to the same DocumentRoot and duplicate your rules or load them from a common file, either via .htaccess or a Include directive.
You can also add the short domain name to your main domain ServerAlias
<VirtualHost *:80>
# This should point to your current config
DocumentRoot /var/www/artists_profiles/
ServerName example.com
ServerAlias www.example.com ex.com www.ex.com
</VirtualHost>
Then you add the new rules to the top of the file:
RewriteBase /
RewriteCond %{HTTP_HOST} ex.com
RewriteRule ^(^.*) artist/$1
Then your existing rules should pick up. If you want to avoid the shortened domain from responding to the longer urls you could add a similar RewriteCond to the existing rules:
RewriteCond %{HTTP_HOST} !ex.com

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]

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>

Apache Redirect Rule Needed.Thanks!

everyone
I have one problem here.
My site is powered by Mediawiki,
which means, the visitors are supposed to visit links like this:
hxxp://www.example.com/wiki/SOMETHING
I want to redirect the user to the Main Page if he is trying to access something else:
hxxp://www.example.com/NOTwiki/SOMETHING -> hxxp://www.example.com/wiki/Main_Page
and
hxxp://www.example.com/NOTwiki -> hxxp://www.example.com/wiki/Main_Page
And "/wiki" is case sensitive,
which means:
hxxp://www.example.com/WiKi/SOMETHING -> hxxp://www.example.com/wiki/Main_Page
I am using a virtual host like this:
<VirtualHost 12.34.56.78:80>
ServerName example.com
ServerAdmin admin#example.com
ServerAlias www.example.com
DocumentRoot /srv/www/example/public_html/mediawiki/
ErrorLog logs/AP/error_log
</VirtualHost>
I am new on Apache and still learning.
Be specific,Please.
Thanks a lot!
This config should be:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteCond %{REQUEST_URI} ^index.php
RewriteRule .* - [L]
RewriteRule !^wiki(/.*)?$ wiki/Main_Page [R=301,L]
</IfModule>
The RewriteCond/RewriteRule says "If the start of the path is not /wiki, rewrite all URLS to /wiki/Main_Page using a 301 redirect (R=301), and do not process any more rules (L)"