Forum Moderators: phranque
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]
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>
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]
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
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.
The answer was:
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+ \.example\.com
RewriteRule (.*) /replica/index.pl/%2$1 [L]