virtual hosts map to directory structure - apache

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>

Related

URL mapping between 2 domains

We have an online service with 3 customers: Autoparts, CarExamples and Cars & More. This is the url structure:
example.com/autoparts
example.com/carexamples
example.com/carsandmore
One of the customers (CarExamples) wants to use their own domain (carexamples.com), but it has to load the content of example.com/carexamples, of course always using the original domain.
That means that:
carexamples.com should load example.com/carexamples
carexamples.com/about should load example.com/carexamples/about
carexamples.com/categories/trucks?p=1 should load example.com/carexamples/categories/trucks?p=1
We are using a LAMP environment (PHP v5.6). The domain example.com is located in /www/example/html in our server.
The current status of this issue is:
Create an A record in the domain's DNS manager console that points to the same server where example.com is being hosted.
Add a virtual host for the custom domain
<VirtualHost servername:80>
ServerName carexamples.com
ServerAlias www.carexamples.com
DocumentRoot /www/example/html
</VirtualHost>
But this would just load the same content of example.com when opening carexamples.com
It is worth noting that /www/example/html/carexamples is not a directory, it is a rewrite from /www/example/html/store.php?store=carexamples
Shopify does something similar: https://help.shopify.com/en/manual/domains/add-a-domain/using-existing-domains/connecting-domains#set-up-your-existing-domain-to-connect-to-shopify maybe that could help as a reference to this issue.
These are the existing rewrite rules in the .htaccess file:
# stores
RewriteRule ^([0-9a-zA-Z-]{2,50})$ /store.php?store=$1 [QSA,L]
# stores > categories
RewriteRule ^([0-9a-zA-Z-]{2,50})/([0-9a-zA-Z-]{1,50})$ /store.cat.php?store=$1&cat=$2 [QSA,L]
# stores > categories > items
RewriteRule ^([0-9a-zA-Z-]{2,50})/([0-9a-zA-Z-]{1,50})/([0-9]+)/([0-9a-zA-Z-]{1,100})$ /store.cat.php?store=$1&cat=$2&id_item=$3&item=$4 [QSA,L]
Have your rules like this in site root .htaccess:
RewriteEngine On
# per domain rewrite
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} !^(?:www\.)?example\. [NC]
RewriteCond %{HTTP_HOST}:%{REQUEST_URI} ^(?:www\.)?([^.]+)[^:]*:(?!/\1/) [NC]
RewriteRule ^ %1%{REQUEST_URI} [L]
# stores
RewriteRule ^([\w-]{2,50})/?$ store.php?store=$1 [QSA,L]
# stores > categories
RewriteRule ^([\w-]{2,50})/([\w-]{1,50})/?$ store.cat.php?store=$1&cat=$2 [QSA,L]
# stores > categories > items
RewriteRule ^([\w-]{2,50})/([\w-]{1,50})/(\d+)/([\w-]{1,100})/?$ store.cat.php?store=$1&cat=$2&id_item=$3&item=$4 [QSA,L]
If I understood your post correctly (correct me or clarify if this isn't what you're looking for)
You need a separated vHost for each entry .. You're looking at the wrong directory -- This is an example -- Though it should be self explanatory .. Just add /carexamples to the DocumentRoot
<VirtualHost servername:80>
ServerName carexamples.com
ServerAlias www.carexamples.com
DocumentRoot /www/example/html/carexamples
</VirtualHost>

In my .htaccess I want to Rewrite the url ONLY in case there is no subdomain in the url

I would like example.com to rewrite to www.example.com but only when there is no subdomain in the url:
example.com -> rewrite to www.example.com
s.example.com -> no rewrite
peace.example.com -> no rewite
etc..
etc..
All the subdomain rewrites I found add the www regardless of it having a subdomain or not and that is messing me up. I do not want it to redirect to www.c.example.com
You can use two virtual hosts and define for each a different rewrite rule.
<VirtualHost *:80>
ServerName example.com
# rewrite to www.example.com
</VirtualHost>
<VirtualHost *:80>
ServerName *.example.com
# do not rewrite
</VirtualHost>
Edit:
This requires the use of virtual host files though.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
Flags:
NE - No encoding
L - Last rule
R=301 - Redirect with http status=301
RewriteCond %{HTTP_HOST} ^example\.com$ will make it execute when domain is example.com.
References:
Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details

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

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>

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)"