Apache virtual host with regex - apache

I have one domain: www.abc.com
I want to use Apache virtual host to get this:
www.abc.com/index.html?id=m1 ,www.abc.com/index.html?id=m2
to visit
/home/m/index.html
and
www.abc.com/index.html?id=a1 ,www.abc.com/index.html?id=a2
to visit
/home/a/index.html
Can I use apache regex to complete this without .htaccess, like this:
<VirtualHost (www.abc.me/*?id=a*):80>
DocumentRoot /var/www/html/laohu_v1
ServerName www.abc.me
ServerAlias www.abc.me/*?id=a*
</VirtualHost>

First, you only need or should use .htaccess when you don't have full access to your server. Vhost doesn't require to be put in .htaccess. Our Apache config uses none at all ;)
Here is how your vhost could look like. But if you only host one domain with entrance over one port on your server, leave out the <VirtualHost> directive and put the rewrite directly in your server's config.
<VirtualHost *:80>
DocumentRoot /var/www/html/laohu_v1
ServerName abc.me
ServerAlias www.abc.me
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=(.*)\d$
RewriteRule ^(.*) /home/%1/index.html
</VirtualHost>
RewriteCond %{QUERY_STRING} ^id=(.*)\d$ change that to (.) if only ONE character is in front of the digit. And if you need more than one digit, change that to: \d+

Related

Match URL to a specific file in Apache

I have a domain which is currently redirecting to another page. This is my virtual hosts file:
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / http://www.anotherdomain.com
</VirtualHost>
Now I need an SSL certificate for that page, and to verify I must provide a file under a certain URL.
Is it possible to provide the apache configuration that matches e.g. example.com/verify_file_123.txt to a file on my filesystem, e.g. /var/www/my_site/verify_file_123.txt while still having redirects for all other routes?
I tried it with
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Alias "/verify_file_123.txt" "/var/www/my_site/verify_file_123.txt"
Redirect permanent / http://www.anotherdomain.com
</VirtualHost>
but that doesn't work.
Redirects are processed before Aliases are processed, and therefore a request that matches a Redirect or RedirectMatch will never have Aliases applied.
You can try something like this:
Alias "/verify_file_123.txt" "/var/www/my_site/verify_file_123.txt"
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/verify_file_123.txt
RewriteRule ^ http://www.newdomain.com%{REQUEST_URI} [R=301,L]

rewrite url with virtual host

I have very limited experience with system administration and I'm currently trying to re-route/write (which ever is the appropriate term) sub-domains to top level domains. For example, I have a domain called bar.com, and I've created a number of sub-domains such as foo.bar.com. I'd like foo.bar.com to resolve to foo.com which I also own. Foo.com has an A record that points to the IP address where foo.bar.com resides.
If it sounds like I'm not describing this correctly, it's because I'm still learning how this all works.
The following is the foo.bar.com virtual host file.
foo.conf:
<VirtualHost 127.0.0.1:81>
Define DOCUMENT_ROOT /PATH/TO/PUBLIC/APP
Define PHP_PORT 22844
Define PHP_PROXY_URL fcgi://127.0.0.1:${PHP_PORT}
ServerAdmin webmaster#
DocumentRoot ${DOCUMENT_ROOT}
ServerName server-foo
ServerAlias foo.bar.com
ServerAlias www.foo.bar.com
ErrorLog "/PATH/TO/error.log"
CustomLog "/PATH/TO/access.log" common
</VirtualHost>
My question is how would I modify this file to make it so that foo.bar.com resolves to foo.com.
It sounds like you're looking for a way to redirect within the virtual host.
If that's the case then, you should be able to use mod_rewrite within your virtual host to accomplish this.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo\.bar\.com$ [NC]
RewriteRule ^(.*) http://foo.com/ [L,R]
This thread might help: https://serverfault.com/a/120507
Edit: To have foo.com be the primary URL, use something like the below.
<VirtualHost 127.0.0.1:81>
ServerName foo.bar.com
ServerAlias www.foo.bar.com
Redirect "/" "http://foo.com/"
</VirtualHost>
<VirtualHost 127.0.0.1:81>
Define DOCUMENT_ROOT /PATH TO CURRENT FOO.BAR.COM/
[your other settings]
ServerName foo.com
</VirtualHost>

Wild card Sub-domain using virtual hosts

Can someone explain to me how this actually works. This is the code in Apache's httpd.conf file.
<VirtualHost *:80>
DocumentRoot "somepath"
ServerAlias mobile.mysite.com
ServerName mobile.mysite.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "somepath"
ServerAlias *.mysite.com
ServerName mysite.com
</VirtualHost>
I want do something like if its mobile.mysite.com it should go to mobile.mysite.com. If its anything else.mysite.com, then it should go to mysite.com. Does the above code perform the same thing ? Any suggestions ?
No, this code does not do any of the mentioned task. You will have to use mod_rewrite for this purpose.
Modify your httpd.conf file to include mod_rewrite.so and then write something like:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www [NC]
RewriteRule $ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
What the above rewrite rule does is that it checks whether the host name starts with www or not, if it does not then it includes www. in the hostname and redirects.
You could write a rewrite rule similar to this.
Read more about rewrite rule here

How to do an Apache HTTP server virtual host blanket redirect with exceptions

I am running an Apache HTTP server that accepts requests for my domain. Lets call it www.mydomain.com. I have several sites that run under this domain and I have virtual hosts set up for each one that forwards the user depending upon with URL they use. Examples would be a.mydomain.com, b.mydomain.com, etc. I am aware that this may not have been the best way to accomplish this goal considering there are over 100 virtual hosts, but it is something I have inhertited and.. to put it short.. it works. Now on to my problem.
I have recently been tasked with shutting down most of the sites running under the domain. The sites that are shut down have been redirected to one page on my new domain (www.mynewdomain.com). What I have done is changed each of the virtual hosts from something like this:
<VirtualHost *:80>
ServerName a.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteA [PT]
</VirtualHost>
to this:
<VirtualHost *:80>
ServerName a.mydomain.com
Redirect permanent / http://www.mynewdomain.com/replacement
</VirtualHost>
So now I have a list of 100 hosts that all redirect to the same place. What I need to know is if there exists a way to tell the system to redirect all sites at *.mydomain.com to my new page except certain ones. So some entry like this:
<VirtualHost *:80>
ServerName *.mydomain.com
Redirect permanent / http://www.mynewdomain.com/replacement
</VirtualHost>
<VirtualHost *:80>
ServerName h.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteH [PT]
</VirtualHost>
<VirtualHost *:80>
ServerName v.mydomain.com
RewriteEngine On
RewriteRule ^/$ /SiteV [PT]
</VirtualHost>
Which means that every URL you see will go to "http://www.mynewdomain.com/replacement" except "h.mydomain.com" and "v.mydomain.com". The virtual hosts for "h" and "v" must still be intact because I have RewriteRules for each that must continue to work (additional rewriterules not seen in my examples).
Thanks in advance for the assistance!

hosting multiple websites on apache under one ip address

I am trying to host multiple websites (app.diff1.com and app.diff2.com running on tomcat server) on my apache server which are running on ports 8082 and 8083 respectively, to access them i want to use a single domain name (app.in.xxx.com/diff) along with a differentiater (diff1/diff2).I used url rewriting to change the domain names respectively. Now when i am trying to access the websites i always end up with hitting the first virtual host.
Please suggest me if i am not approaching the solution correctly.
Code snippet:
in httpd.conf file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^app\.in\.xxx\.com/([a-z]*)$
RewriteRule ^ http://www.app.%1.com [L,R=301]
</IfModule>
<IfModule mod_proxy.c>
ProxyRequests off
</IfModule>
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.app.diff1.com
ProxyPass / http://www.app.diff1.com:8082/
ProxyPassReverse / http://www.app.diff1.com:8082/
</VirtualHost>
<VirtualHost *:80>
ServerName www.app.diff2.com
ProxyPass / http://www.app.diff2.com:8083/
ProxyPassReverse / http://www.app.diff2.com:8083/
</VirtualHost>
Move your rewrites into the first listed virtual host, and stop trying to capture the first path component of the URL as if it were part of HTTP_HOST -- capture it in the RewriteRule itself and use $1.
Also see ServerPath which is an esoteric way to do the same mapping.