Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite for subdomains w/o WWW

using mod_rewrite for page replication script

         

sy2k

3:49 am on Feb 16, 2004 (gmt 0)

10+ Year Member



I need some professional help. I have studied all the fine points of the Apache mod_rewrite and the myriad of ways to employ its use. But I can't seem to find any reference to my problem.

I am using mod_rewrite to create replicated web pages for affiliates. Here is how I use it in the config file:

Rewritelog logs/rewrite.log
RewritelogLevel 0

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.[^.]+\.example\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^www\.([^.]+)\.example\.com(.*) /replica/index.pl/$1$2

The code above allows this link to work as a replicated web address with the WWW:
http://WWW.vitality.example.com

But it won't work for the address without the WWW:
http://vitality.example.com

The first link, with the WWW, calls the index.pl script and the index2 page appears replicated for the affiliate. But without the WWW, it defaults to the index page.

What I need is to figure out a way to get the mod_rewrite to allow the use of subdomain URL's without the WWW. Basically, a second variation of the code above.

Any help out there for this?

Thanks!
Syris :-)

[edited by: jdMorgan at 3:57 am (utc) on Feb. 16, 2004]
[edit reason] Examplified URLs; Please see TOS. [/edit]

Kandevil

4:02 am on Feb 16, 2004 (gmt 0)

10+ Year Member



Hi sy2k and welcome to Webmasterworld [webmasterworld.com], you might want to remove the specifics before one of the mods does for you, as per the tos [webmasterworld.com].

I'm sure somebody will be along to help you out with the mod_rewrite, but I can't :D

<added>
See jdMorgan beat me to it :)
</added>

jdMorgan

4:22 am on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Syris,

Welcome to WebmasterWorld [webmasterworld.com]!

I won't pretend to have grasped all the possible subtleties of your code, but I believe you can simplify it to something like this:


RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.example\.com
RewriteRule (.*) /replica/index.pl/%2$1 [L]

The first set of parentheses in the RewriteCond delimits the optional substring "www\." only, so that the "?" applies to the entire substring and makes it optional; Although this also creates a back-reference (%1), this is not used.
The second set of parentheses in the RewriteCond creates a back-reference (%2) to the required subdomain.
The RewriteRule then copies the subdomain name %2 and requested resource name ("filename") $1 to the end of the script path.

Two caveats: First, beware of allowing links using uppercase characters, as this will require extra work in the script and is very difficult to fix by other means; And second, always use the [L] flag on rules unless you have a known-good reason not to. In 99% of all cases, once a URL is rewritten, there is no need to waste CPU time looking for further RewriteRule matches. The [L] flag will terminate mod_rewrite processing for this HTTP request if the RewriteRule it accompanies is invoked.

This method does not require chaining and makes the "www." optional. If it does not meet your needs, perhaps it can serve as an example of an available shortcut, and a demonstration of one technique to make part of a pattern optional.

Jim

sy2k

7:03 pm on Feb 16, 2004 (gmt 0)

10+ Year Member



Thanks jdMorgan for your input. The rule you provided does work for the replication function, kinda. It does make the WWW optional in the replicated subdomain URL's but with some side effects.

With the rule you are always treating the URL as a subdomain even when visiting the site www.example.com. So because of this, the default page (index.html) of the website will no longer work and all of the images on the replicated site stop working because there is no longer any route to the images or pages in the the default root folder.

(background) My site is replicated for affiliates. The index.html page is not an entry to the site, it stops people and asks for a subdomain name of a referring affiliate. Therefor, the only access to the full site is through a URL like abc.example.com or www.abc.example.com. Those url's call up the index2.html page through the index.pl script and display the front page of the site replicated for the affiliates...

Any idea on a work around for this? I need the WWW to be optional in the replicated subdomain name url's but I need the images to show up in the replicated pages. Plus I need the index.html page to show when someone goes to www.example.com or example.com.

jdMorgan

11:15 pm on Feb 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If I understand the non-subdomain problem correctly, you can disable the rewrite for that case by preceding the code above with:

RewriteCond %{HTTP_HOST} !^www\.example\.com

Jim

sy2k

9:36 am on Feb 17, 2004 (gmt 0)

10+ Year Member



Thanks jdMorgan, that did the trick! The www is optional in the URL's, the index.html page works and the images load in the replicated page templates!

The answer was:


RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+ \.example\.com
RewriteRule (.*) /replica/index.pl/%2$1 [L]

You are a true master!