Hosting Multiple sites on a single IP address using apache Mod_rewrite.

Posted by Jay     26 September, 2009    3,297 views   

mod_rewrite_apache_httpd_htaccess.jpgThis is my notes, it will be refined very soon. Mod_rewrite, for those that don’t know, is a powerful module that allows you to handle incoming URI requests and more.
This article will explain the use of and advantages of the MaxRedirects option. Web hosting companies that offer the mod_rewrite module should take note of these reasons to upgrade to the latest Apache release. Keeping your Web server up-to-date is a must. Hosting companies that do not offer the mod_rewrite module are encouraged to enable it.
Can I host multiple domains on one account?
Yes, using our Domain Alias service.
A Domain Alias allows additional domain names to all lead to same account. There are several ways you can use this service:
Multiple Domains, One Website
For example, if you are hosting your domain name “original.com” with us and added a Domain Alias for “alias.com”, then http://www.original.com and http://www.alias.com would take a visitor to the exact same pages of your original.com website. http://original.com/somefile.html would bring someone to the exact same page as http://alias.com/somefile.html.
Multiple Domains, Multiple Websites
Using this method, you can host an additional website within your account for each Domain Alias you add. This is a custom configuration requiring a couple extra steps.
After adding the Domain Alias in OnSite, then place an .htaccess file in /htdocs/www of the following example format:

RewriteEngine On
RewriteCond %{HTTP_HOST} alias.com$ [NC]
RewriteCond %{REQUEST_URI} !^/alias/.*$
RewriteRule ^(.*)$ /alias/$1

I currently have multiple domains pointed to my hosting account with each domain having its own document root:
mainsite -> /home/user/public_html/
site1.com -> /home/user/public_html/site1
site2.com -> /home/user/public_html/site2

RewriteEngine on
Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/site1%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?site1\.com$ [NC]
RewriteRule ^(.*)$ /site1/$1 [QSA,L]

Here is the sample .htaccess to host multiple domains on a single ip at port other than 80
RewriteEngine On
Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^(www\.)?lightsql\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule (.*) /var/www/html/lightsql/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?notesbit\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule (.*) /var/www/html/notesbit/$1 [L]

RewriteCond %{HTTP_HOST} ^ branfordmagazine\.notesbit\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule (.*) /var/www/html/ branfordmagazine/$1 [L]

Regular Expression Explanation:

* ^: pattern should match from the beginning of the string
* (: start of subpattern
* [A-Za-z0-9-]: class of characters
* +: one or more times
* ): end of subpattern
* /: foward slash…
* ?: can occur zero or once
* $: end of string

Here is the sample .htaccess to host multiple domains on a single ip at port 80
RewriteEngine On
Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^(www\.)?lightsql\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/lightsql%{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?lightsql\.com$ [NC]
RewriteRule ^(.*)$ /lightsql/$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^ branfordmagazine\.notesbit\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/ notesbit %{REQUEST_URI}/ -d
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^ branfordmagazine\. notesbit \.com$ [NC]
RewriteRule ^(.*)$ /branfordmagazine /$1 [QSA,L]

Issues faced:

mod_rewrite was loaded and yet it was not working in .htaccess files (and httpd.conf initially).
Here is the solution along with how you can debug mod_rewrite problems.

Debugging procedure:
Add these two line to your httpd.conf immediately after RewriteEngine On.
RewriteLog “/var/log/httpd/rewrite_log”
RewriteLogLevel 9
After that I added the required RewriteRule etc.

The reason for testing directly in httpd.conf is to ensure that mod_rewrite is working in the first place.
After some debugging I realized my expression was wrong. So now I found mod_rewrite was working in httpd.conf.
However it still wasn’t working in .htaccess files.

Solution:
I found AllowOverride was set to None in httpd.conf.
I changed it to All (after all I am the only user of the machine).
And it finally started working everywhere (after a restart).

What I learnt:
Unmanaged dedicated web hosting is really really painful, unless you are (or have) a good linux system administrator.
Too many things to setup and too many points of failure. And I haven’t even started working on serious hardening

I am trying to use mod_rewrite in Apache.
When I try to turn on rewrite logging I get the “Rewritelog not allowed here” message.

RewriteLog and RewriteLogLevel are Server/Virtual Host config directive that cannot be used inside or structures.
Move them outside this block and restart the server.
That directive must be set globally at server level or in the VirtualHost-container for the hostname

References:
http://www.webmasterworld.com/apache/3437051.htm
http://articles.sitepoint.com/article/guide-url-rewriting/3
http://httpd.apache.org/docs/1.3/misc/rewriteguide.html
http://corz.org/serv/tricks/htaccess2.php
http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html Status Code Definitions
http://www.webmasterworld.com/forum92/2984.htm Simple rewrite condition
http://www.widexl.com/tutorials/mod_rewrite.html
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
http://www.klauskomenda.com/code/rewrite-rules-practical-examples/
http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html
http://www.bigresource.com/PHP-Parked-Domains-and-mod_rewrite-in-htaccess-07wqO9ey.html
http://articles.sitepoint.com/article/apache-mod_rewrite-examples
http://forum.modrewrite.com/viewtopic.php?p=53500&sid=3a74ed77ffec0df9715988f5957781f2
http://www.phpfreaks.com/forums/index.php?topic=237646.0

Following Google Searches Lead To This Post: apache rewriterule ip
RewriteCond %{HTTP_HOST} IP Adress
mod rewrite url and ip
multiple virtual hosts single port single ip address apache
apache mutiple ports
apache host 2 website on one ip
hosting 2 websites on 1 ip apache bind
virtualhost mod_rewrite setting centos
hosting multiple apache on one ip
multi hosting with in single ip
linux appache single ip address with multiple sites
centos setup multiple websites
binding multiple ip address to a single domain
single ip multiple websites linux appache
rewrite condition multiple domains
apache virtual host two adressess ip rewrite
enabling 2 domains on a single apache server
mod_rewrite multiple hostnames
use mod_rewrite multiple servers behind one ip
httpd.conf Configuring Multiple ip address
multiple website 1 ip unix
.htaccess RewriteRule multiple domains
how to set multiple hostname on single domain
mod_rewrite multiple domains
rewrite condition host
apache url rewrite hostname multidomain
htaccess multiple rewritecond & rewriterule
advantages hostname multiple ip addresses
“multiple apache” “one ip” different url
apache single ip 2 websites
apache multiple rewrite
apache htaccess alias http_host
how to port block multiple ip except one in linux
one ipaddress with muliple domain names apache
faire fonctionner apache2 url rewriting “rewritelog not allowed here”
htaccess rewrite multiple domains in one root
host multiple web services on single ip address
rewritecond ip
centos apache multiple sites
rewrite rule with multiple conditions, request_uri
multiple sites in apache in CentOS
multiple rewrite under one domain apache
multipe domains modrewrite
more site on one webhosting HTTP_HOST
apache domains lead to same website
httpd.conf multiple sites one ip
setting up multiple websites on single IP address
multiple domains one host mod_rewrite
hosting multiple sites on a single domain
call script apache mod_rewrite
apache single ip multi site
multiple domain name single ip apache
multiple apache linux
htaccess multiple conditions
centos set multiple hostnames
apache rewriterule depending on source IP
htaccess “RewriteCond %{HTTP_HOST} !^”
multiple application using single using apache
run multiple websites on single IP address in Apache
rewritecond apache ip address
“multiple apache” “1 ip address”
apache host multiple wordpress sites
multiple sites on sigle apache
multiple domains single IP htaccess
apache host multiple domains on same port and ip
apache multiple urls
songle port multiple ip
htaccess mod_rewrite ip-adresse
apache rewritecondition -f
hosting multi ip address
set multiple hostnames from one ip linux
mod_rewrite port other than 80
hosting procedure 2 sites in same IP
hosting procedure 2 sites in same IP Apache httpd.conf
hosting multiple sites on a single machine apache centos
how to create multiple websites with one ip address in linux
apache statistic multi domains
linux hosting multiple ip addresses
RewriteCond %{HTTP_HOST} !^(*
mod_rewrite url certain IP
host multiple websites on one server linux
apache multiple sites one server same domain
apache rewritecond multi urls
running several sites using apache on the same IP address
apache multiple sites single port
centos multiple ip alias
htaccess conditions ip addresses
apache multiple site hosting
host multiple website in httpd.conf
how to run two sites in apache2 on single ip
host mutiple websites
virtual host mod_rewrite user;centos
wordpress multidomain mod rewrite
httpd.conf multiple sites ip
how to host two domains on one ip
example rewrite apache ip url 80
hosting multiple domains on IP apache rewrite rules
mod_rewrite ip
apache multiple rewritecond
apache mulitple IPs single port
mod_rewrite wordpress multiple sites
rewritecond multiple domain
apache rewrite host to ip
hosting multiple websites on single linux
hosting multiple websites on different ports
hosting multiple websites on linux
rewriterule http ip address
mod_rewrite multiple domains single host
single ip multi domain for apache
multiple websites on one ip address
howting multiple websites on one server with one ip
apache2.2 running two websites from one server one ip
domain alias and htaccess multiple domain hosting
multiple web hosting centos
.htaccess “multiple domains” “one root”
domain alias htaccess multiple domain
apache httpd
multiple ip address advantage
multiple url on one ip php
ip aliasing linux
apache virtual host “source ip”
rewrite multiple domains single ip
how to block websites using apache rewrite rule
mod_rewrite multiple conditions
more websites one ip-adress apache
apache multiple domain mod_rewrite
htaccess: RewriteLogLevel not allowed here
hosting multiple website one ip using apache
apache multi ~ username sites
RewriteLogLevel not allowed here
host 2 domains with one ip centos
multiple website one ip address linux
apache rewritecond match IP address
cento apache multiple websites
bind muliple domain name to ip
centos apache host multiple domains
mod_rewrite multiple root
apache rewrite url ip adress
apache rewrite examples “source ip”
mod_rewrite cond host
apache run 2 site under 1 virtual host with mod_rewrite
centos apache rewrite not working
MOD BLOCK IP ADRESSE
httpd.conf multiple site hosting
host 2 websites with 1 IP address on Linux server
multiple websites on same domain name apache
centos multiple domain in the same server
how to host multiple sites using apache2.2
apache2 rewrite single IP
wordpress mod_rewrite not allowed
php hosting rewrite not allowed
hosting several websites on one linux machine
htaccess multi-host
wordpress multiple apache
host multiple sites on centos
Multiple Websites on single IP in linux
RewriteCond multiDomain RewriteRule httpd.conf
rewritecond source IP
multiple Domains on a single IP Apache
mod_rewrite multiple run
Multiple IP Addresses on a Single NIC centos port problem
apache mulitple sites
centos multiple websites
htaccess mod_rewrite

Post to Twitter  Post to Delicious  Post to Digg    Post to StumbleUpon

Categories : Technology, Web & Scripts, wordpress Tags : ,

Comments

No comments yet.


Leave a comment

(required)

(required)