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>
Related
How to rewrite if file after stylesheets then read file under specific folder
e.g
if user visit
domain1.com/stylesheets/index.css actually read > domain1.com/app/assets/stylesheets/index.css
or
if visit
domain1.com/stylesheets/bundle/index.css read > domain1.com/app/assets/bundle/stylesheets/index.css
I tried below code but not work...
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com\.localhost\$ [NC]
RewriteRule ^/stylesheets/(.*)\.css$ app/assets/stylesheets/$1
#or this rule not work too
RewriteRule ^/stylesheets/$ app/assets/stylesheets/$1
<VirtualHost *:80>
DocumentRoot "/Users/username/Sites/domain1.com"
ServerName domain1.com
</VirtualHost>
First you need to check the HTTP_HOST against the host name (without any further stuff, that is):
RewriteCond %{HTTP_HOST} ^domain1\.com\$ [NC]
Second, in order to be able to process the query further, you will need to capture parts of it (in parentheses, that is).
You can use this rule in vhost config or in site root .htaccess:
RewriteEngine On
RewriteRule ^/?(stylesheets/.+?\.css)$ /app/assets/$1 [L,NC]
If using vhost config remember to restart Apache after making this change. This is assuming app/assets/ path exists as full filesystem path of /Users/username/Sites/domain1.com/app/assets/
I am running an apache webserver on a linux EC2 instance.
The problem is that you can access the server using the IP address, DNS and the domain name. This causes a problem for SEO and I want to tidy it up.
I have read on the apache documentation that you can do a mod_rewrite and this needs to be done in the httpd.conf if you have root access otherwise in the .htaccess for per directory override.
I have root access so I am trying to change the httpd.conf
If the user types in
http://52.17.12.123/
or
http://ec2-52.17.12.123.eu-west-1.compute.amazonaws.com/
I want them to be redirected to
www.example.com
This is what I tried
<VirtualHost *:80>
DocumentRoot "/var/www/html/my-website"
# Other directives here
RewriteEngine On
RewriteCond %{HTTP_HOST} !^52.17.12.123.com$
RewriteRule /* http://www.example.com/ [R]
</VirtualHost>
It seems to partially work but www.example.com does not load due to to many redirects.
--EDIT--
Thanks, so now in my httpd.conf I now have the following configuration
Listen 80
NameVirtualHost *:80
DocumentRoot "/var/www/html/my-website"
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)$ [NC]
RewriteRule ^/(.*)$ http://www.example.com [R=301,L]
It is all working correctly now
It seems to partially work.
I doubt, considering the rule you currently have in your httpd.conf.
You can have it this way
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301,L]
I have a VirtualHost with many ServerAlias domains defined. Using mod rewrite in the VirtualHost, I would like the following to occur:
sub.abc.com changes to sub.abc.com/?client_id=ABC
sub.def.com changes to sub.def.com/?client_id=DEF
sub2.abc.com changes to sub2.abc.com/?client_id=GHI
[... and so on ...]
My current configuration is:
<VirtualHost 1.2.3.4:8080>
ServerName sub.abc.com
ServerAlias sub.def.com sub2.abc.com
DocumentRoot /var/www/prod/
[... some other things ...]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.abc\.com
RewriteRule ^/([0-9]+)/?$ login.php?client_id=ABC [NC,L]
RewriteCond %{HTTP_HOST} ^sub\.def\.com
RewriteRule ^/([0-9]+)/?$ login.php?client_id=DEF [NC,L]
RewriteCond %{HTTP_HOST} ^sub2\.abc\.com
RewriteRule ^/([0-9]+)/?$ login.php?client_id=GHI [NC,L]
</IfModule>
</VirtualHost>
But this doesn't actually do the rewrite .. I am pretty sure my Cond or Rule is messed up (lol .. I guess that is pretty obvious). Any ideas?
Suggestion :
Let's say your domain name sub.abc.com is pointing to X ip address. Consider creating two additional records sub.def.com and sub2.abc.com under the same ip address X.
This practice is called as Shared Web Hosting
This way you can maintain separate configuration files for your three different domains.
So if a Client accesses sub2.abc.com - the configuration file for sub2.abc.com will come into picture and request will be served accordingly.
I hope this helps! Good Luck!
I'm currently trying to redirect my local configured domains to its directories.. What is the best way to achieve this using htaccess?
Currently 'localhost' redirects to /Library/WebServer/Documents/ (mac OSX), i want to create an htaccess file in this directory that will lead all domains to its directory.
Example htaccess code of what I'm looking for:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com/$
RewriteRule ^(.*)$ /Library/WebServer/Documents/example.com/$1 [L]
I hope someone can help me out, think there's an easy fix for this!
Kind regards,
This is not possible with .htaccess alone. You need access to the main server configuration file or at least modify the appropriate virtual host section.
If you can modify the main config file, you can add any number of virtual hosts.
If you're allowed to adjust your virtual host only, you must add as many ServerAlias entries to your virtual host as you have domains.
Then you can add the rewrite rules for the various domains
RewriteEngine On
# prevent endless loop
RewriteCond %{ENV:REDIRECT_STATUS} .
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/example.com/$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com/$
RewriteRule ^.*$ /Library/WebServer/Documents/domain.com/$0 [L]
Try this
# get the server name from the Host: header
UseCanonicalName Off
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /var/www/vhosts/%0/httpdocs
<Location /var/www/vhosts>
AllowOverride All
Options +FollowSymLinks
</Location>
Then, you could try these examples:
example.com would show /var/www/vhosts/example.com/httpdocs
example2.com would show /var/www/vhosts/example2.com/httpdocs
Remember to add the domains to your hosts file.
Source
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>