Hosting Multiple sites on a single IP address using apache Mod_rewrite.
This 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
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} ^(www\.)?notesbit\.com [NC] RewriteCond %{HTTP_HOST} ^ branfordmagazine\.notesbit\.com [NC] |
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 %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{HTTP_HOST} ^ branfordmagazine\.notesbit\.com$ [NC] RewriteCond %{ENV:REDIRECT_STATUS} ^$ |
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
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

Comments
No comments yet.