Different URL based on same server but different DNS entry - apache

We have a server, say 192.168.1.5, and we have two DNS entries for it:
server.domain.com
ftplogs.domain.com
Both of which point to the server's IP.
I'd want to have server.domain.com redirect to server.domain.com/page1, and I'd like to have ftplogs.domain.com redirect to ftplogs.domain.com/logs.
Any ideas on how to pull this off? Have been fighting with mod_rewrite rules for a bit now.
Thanks!

you can try it:
<VirtualHost *:80>
ServerName server.domain.com
RewriteEngine On
Redirect / http://server.domain.com/page1
</VirtualHost>
<VirtualHost *:80>
ServerName ftplogs.domain.com
RewriteEngine On
Redirect / http://ftplogs.domain.com/logs
</VirtualHost>
This work for me :-)

Related

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.

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>

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
...

add a server alias to the domain's VHOST?

I have a VPS. I hosted a domain ipointing to a sub directory of the www folder. The domain works fine till the home page. The moment I start going to other pages its shows my servers [orginalname]/[subdirectory name] . I think I need to add a server alias to the domain's VHOST. Can anyone tell me how to do that??
Are you using Apache?
Try with
<VirtualHost *:80>
DocumentRoot "/path/to/document/root"
ServerName name1
ServerAlias name2
...
I did it using the proxy Apache option, this is it:
My VirtualHost is http://dlx/ and I want to add an "alias" like http://dlx/drupal/
In the httpd.config file I added a proxy configuration:
<VirtualHost 127.0.0.1>
ServerName dlx
DocumentRoot "C:/deluxe/"
<LocationMatch /drupal/>
ProxyPass http://localhost/drupal/
ProxyPassReverse http://localhost/drupal/
</LocationMatch>
</VirtualHost>
Configure .htaccess on my dlx virtualhost (C:/deluxe/):
RewriteRule ^drupal/(.*)$ http://localhost/drupal/$1 [P,L]
That's it. It works for me, I hope it also works for you.