rewrite url with virtual host - apache

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>

Related

Apache2 multiple domains to use same document root

Trying to get multiple domains (potentially dozens) to use the same document root as I want laravel to take care of all the routing.
Sites will be custom domain names ie. johnwilson.com, davidsmith.com, lisabrown.com and laravel will display a templated page. I do not want the URL rewritten in the address bar to the user.
I can't get apache2 to respect my virtual host configuration though, especially using SSL.
The configuration is a LAMP stack on Ubuntu. I have two other runrelated sites already running successfully on this server, using two seperate document roots. These are proxied through cloudfare.
These "templated" pages I'm just going to use lets encrypt for though.
I've tried:
2 seperate virtual hosts.
<VirtualHost *:443>
ServerName johnsmith.com.au
DocumentRoot /var/www/microsites/public
# letsencrypt certificate details here
</VirtualHost>
<VirtualHost *:443>
ServerName lisabrown.com.au
DocumentRoot /var/www/microsites/public
# letsencrypt certificate details here
</VirtualHost>
In this case johnsmith.com.au works, but lisabrown.com.au just redirects to johnsmith.com.au. completely rewriting the url in the address bar.
I've tried using ServerAlias aswell but this leads me to various errors, 404, SSL_INSECURE.
What's the correct way to do this? TIA
Using the following conf files (one per domain) I got it to work as expected. (and deleting the letsencrypt auto generated domain-le-ssl.conf file)
<VirtualHost *:80>
ServerName domain.com.au
ServerAlias www.domain.com.au
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com.au [OR]
RewriteCond %{SERVER_NAME} =domain.com.au
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName domain.com.au
ServerAdmin admin#domain.com.au
DocumentRoot /var/www/microsites/public
ErrorLog ${APACHE_LOG_DIR}/microsites/domain.error.log
CustomLog ${APACHE_LOG_DIR}/microsites/domain.access.log combined
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/domain.com.au/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/domain.com.au/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Apache Virtual Hosts Non-www not working

I'm setting up a Virtual Hosts file on my CentOS 7 box and I'm having trouble getting my domain to resolve correctly.
Here's what my current /etc/httpd/conf.d/vhost.conf file looks like
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster#domain.com
ServerName www.domain.com
ServerAlias domain.com
DocumentRoot /var/www/html/domain.com/public_html/
ErrorLog /var/log/httpd/error.log
CustomLog /var/log/httpd/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.domain.com [OR]
RewriteCond %{SERVER_NAME} =domain.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
It seems the the correct redirects are happening. For exmaple:
domain.com redirects to https: //www.domain.com
www works fine
BUT
https: //domain.com doesn't work
http ://domain.com doesn't work
In fact, if I remove the redirects I have set, domain.com ins't working at all, so it looks like the ServerAlias is broken?
I'm wondering if I need another redirect or is there some other step I'm missing?
Also, don't mind the spaces between http and the domain name. StackOverflow made me format it that way.
As presented, no request to anything https will ever work. Normal, you only have a VirtualHost on port 80. You do have a Listen directive for that port right?
For your redirections. It says: if you ask for http://www.example.com or http://example.com, redirect to https://<WHAT THE USER ASKED FOR>. In essence you are forcing your users to use https all the time, no problem there. But you do not have a VirtualHost on port 443, hence no response.
So:
Listen *:80
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
ErrorLog /var/log/httpd/80_error.log
CustomLog /var/log/httpd/80_access.log combined
RewriteEngine on
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
Listen *:443
<VirtualHost *:443>
ServerName www.example.com
# in case users do directly to https
ServerAlias example.com
DocumentRoot /var/www/html/domain.com/public_html/
DocumentIndex index.html
ErrorLog /var/log/httpd/443_error.log
CustomLog /var/log/httpd/443_access.log combined
# SSL CONFIGURATIONS, TODO!
</VirtualHost>
In your *:443 VH, you will have to configure certificates and SSL.
Your certificates will have to be valid for both www.example.com and example.com to avoid browser complaints.
Careful there might be an ssl.conf included file under conf.d that defines some of this. Make sure you only set it once to avoid confusion.
No need to define DocumentRoot in *:80 VH since it only redirects and does not respond content to client.
Have fun!
I solved the issue. I had my local hosts file configured to point to an old out of date IP address……
domain.com *bad ip address*
I'm so embarrassed. I must have set that up months ago and forgot.

Apache virtual host with regex

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+

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!

Redirecting http://example.com to http:/www.example.com while keeping http://subdomain.example.com access intact

I have a website on which I've enabled subdomain access such as:
http://subdomain1.example.com
which accesses the same code, but passing a domain parameter in order to show a different microsite. The httpd.conf code for that looks like this:
RewriteCond %{HTTP_HOST} ^([^./]+)\.example\.com$
RewriteRule forums.html$ /browse.php?type=forums&domain=%1 [QSA]
Now I need to redirect http://example.com to http://www.example.com
I tried this, but it did not work:
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
(source: http://www.cyberciti.biz/faq/apache-redirect-domaincom-to-wwwdomaincom/ )
EDIT1
<VirtualHost IPADDRESS:80>
ServerAlias *.example.com
DocumentRoot /var/www/html/abc
ServerName www.example.com
UseCanonicalName On
EDIT2
Hi mreithub,
The setup I need is something like this:
http://X1.example.com should use the code in /something/X1
http://X2.example.com should use the code in /something/X2
http://example.com should redirect to http://www.example.com
http://www.example.com/scriptA.php should use the code in /var/www/html/abc/scriptA.php
http://whateverelse.example.com/scriptA.php should use the code in /var/www/html/abc/scriptA.php but be passed with a 'domain=whateverelse' parameter (but the URL on screen should show always show the domain as being http://whateverelse.example.com )
I had asked a question on SF - https://serverfault.com/questions/408805/configuring-httpd-conf-to-handle-wildcard-domains-with-multiple-scripts - from where I used adaptr's technique to pass the domain parameter to the PHP scripts.
My personal favorite for redirecting whole VirtualHosts in apache is to simply create a VirtualHost for the domain to redirect and use the Redirect directive:
<VirtualHost IPADDRESS:80>
ServerName example.com
Redirect / http://www.example.com/
DocumentRoot /var/www # <-- Just for completeness
</VirtualHost>
... and then another VirtualHost for your actual website
Redirect redirects every request going to host a to b while keeping any postfixes (e.g. http://example.com/foo?bar=bak becomes http://www.example.com/foo?bar=bak).
I use Redirect a lot to rewrite from http:// to https://
Wow. 3 hours later... Lots of changes, lots of learnings.
1) Changed this:
NameVirtualHost IPADDRESS:80
To:
NameVirtualHost *:80
2) Marked all:
<VirtualHost IPADDRESS:80>
As:
<VirtualHost *:80>
3) Rearranged ServerName and placed it first within the VirtualHost (not sure if this made any difference)
<VirtualHost *:80>
ServerName test4.example.com
ServerAlias test4.example.com
DocumentRoot /home/test4/public_html
UseCanonicalName On
</VirtualHost>
3) Rearranged all VirtualHosts. Placed the 'static' / fixed subdomains earlier and the catch-all / www one as the last one. The last one looks like:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com *.example.com
DocumentRoot /var/www/html/abc
UseCanonicalName On
...