VirtualHost mod_rewrite multiple domains same DocumentRoot - apache

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!

Related

I'm trying to redirect /example to example.html in Apache's httpd.conf, but it doesn't work

Here's the code I'm trying to apply in httpd.conf:
<VirtualHost *:80>
ServerName www.test.com
ServerAlias www.test.com
DocumentRoot /home/test_com
ErrorLog /home/logs/apache/www_test/www.test.com-error_log
CustomLog /home/logs/apache/www_test/www.test.com.-access_log common env=!do_not_log
# https redirect
RewriteEngine On
RewriteCond %{HTTP_HOST} www.test.com
RewriteCond %{REQUEST_URI} /example
RewriteRule . https://www.test.com/example.html [R=303,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}
<Directory "/home/test_com/">
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
After applying the code and restarting apache, when I go to /example I get a 404 not found response.
When I test htaccess at (https://htaccess.madewithlove.be), the output is as https://www.test.com/example.html as I want. I wonder what the problem is.
RewriteCond %{HTTP_HOST} www.test.com
RewriteCond %{REQUEST_URI} /example
RewriteRule . https://www.test.com/example.html [R=303,L]
when I go to /example I get a 404 not found response.
Not sure if something has been lost in your exemplification, but from the rules as written, if you request /example it will result in a redirect-loop as it will continually redirect to /example.html again and again. (Unless you are actually making the request over HTTPS, in which case the directive isn't doing anything at all and you will indeed get a 404!)
This is because the second condition %{REQUEST_URI} /example also matches the target URL /example.html. You need to make the regex more specific and match only /example, not any URL that simply contains /example. This check can also be moved to the RewriteRule directive (the condition is not required). For example:
RewriteRule ^/example$ https://www.test.com/example.html [R=303,L]
The condition that checks the HTTP_HOST would not seem necessary, since these directives are in the vHost for www.test.com, so it cannot be any other hostname?
However, since this rule is in the vHost for port 80, it is only going to apply to HTTP requests (not HTTPS). If you are actually requesting https://www.test.com/example (ie. HTTPS) as would seem to be implied by your test results from the MWL tool, then the directive is never going to be processed and you will indeed get a 404.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}
Your HTTP to HTTPS redirect is missing the last part so redirects everything to the homepage. The RewriteRule directive should read:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
However, there's no point redirecting to HTTPS if all your directives are in the vHost:80 container.
This rule is also more complex than it needs to be. Since you are in the vHost:80 container, HTTPS is always going to be off - the check is redundant.
ServerName www.test.com
ServerAlias www.test.com
If you only have one hostname then you don't need two directives. Only the ServerName is required in this case.
When I test htaccess at (https://htaccess.madewithlove.be)
You don't have an .htaccess file here. You are using the directives directly in the vHost container, which is a different context. The directives behave slightly different here. For example, the RewriteRule pattern . (a single dot) has a different result here than when used in .htaccess. In a virtualhost context it is successful for every request, whereas in .htaccess it is successful for everything except the homepage (which is probably the intention here).
However, the MWL .htaccess tester only makes a "single" request (or single pass through the file). It does not "follow" redirects or make multiple passes through the file as it would do in a real server environment. So it cannot catch redirect/rewrite loops - which are a common cause of error.
The right part on RewriteRule is the destination file, try change :
RewriteCond %{REQUEST_URI} /example
To :
RewriteCond %{REQUEST_URI} /example.html

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>

Apache server config redirect from IP to domain name EC2

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]

Apache httpd.conf - Link multiple domains to corresponding subfolders

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>

Apache configuration / Catch-all subdomain / Specific subdomain exception

Startting with some simple domain setup :
<VirtualHost *:80>
ServerName site.com
ServerAlias *.site.com
DocumentRoot /path/to/site.com
<Directory /path/to/site.com>
AllowOverride all
Options -MultiViews
</Directory>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$ [NC]
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{HTTP_HOST} ^(.*).site.com$ [NC]
RewriteRule ^(.*)$ http://site.com$1 [R=301]
RailsEnv production
</VirtualHost>
So I'm pretty happy, anything put before my domain will be removed and my urls will always be clean : http://site.com
Cool!
** BUT **
I'd like to set a specific subdomain (an exception in that case) which would point to another site on the same server (another folder, another slightly different apache config, etc…)
This setup above will always precedes the one I would do for my 'exception' dubdomain.
How could I reach the goal described ? Catch-all apache setup with one (or some more) subdomain setup exceptions ?
Try to add you configuration for desired subdomain above this code, it should help.