homepage Welcome to WebmasterWorld Guest from 54.197.147.90
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
RewriteCond for multiple RewriteRules
Using multiple mod rewrite rules based on a single RewriteCond
ensteve




msg:4239635
 10:32 am on Dec 7, 2010 (gmt 0)

Hi,

I would like to use multiple mod rewrite rules for a single rewriteCond, more spefically, based on HOST.

To achieve this you must state the conditions each time before use. If you need to use these conditions frequently the htaccess file will become quite large.

Eg:

rewriteCond %{HTTP_HOST} www.example.com [NC,OR]
rewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^1/?$ /index.php?rule=1

rewriteCond %{HTTP_HOST} www.example.com [NC,OR]
rewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^2/?$ /index.php?rule=2

I have a primary domain, which is example.com (and www.example.com) as well as many other domains which are aliases of this domain.

Can htaccess do the following type of statement

<IF HOST = example.com [OR] www.example.com>
RewriteRule
RewriteRule
RewriteRule
RewriteRule
</IF>

<ELSE>
RewriteRule
RewriteRule
RewriteRule
RewriteRule
RewriteRule
</ELSE>

Thanks in advance.

 

jdMorgan




msg:4239691
 2:23 pm on Dec 7, 2010 (gmt 0)

You could use "skip rules" :

# Skip the following four rules if the requested hostname is NOT example.com or www.example.com
RewriteCond %{HTTP_HOST} !^(www\.)?example.com [NC]
RewriteRule ^ - [S=4]
#
# Rules for example.com or www.example.com
RewriteRule blah blah [L]
RewriteRule blah blah [L]
RewriteRule blah blah [L]
RewriteRule blah blah [L]
#
# Skip the following five rules if the requested hostname IS example.com or www.example.com
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^ - [S=5]
#
# Rules for all hostnames _except_ example.com or www.example.com
RewriteRule blah blah [L]
RewriteRule blah blah [L]
RewriteRule blah blah [L]
RewriteRule blah blah [L]
RewriteRule blah blah [L]
#
# Rules to be applied if none of the above rules are invoked.
RewriteRule blah blah [L]

Note that the second "skip rule" is redundant *only* if the last section is empty OR if it is known that one of the preceding RewriteRules is guaranteed to have been invoked. Since this "guarantee" is doubtfully assured, I suggest that you leave that second skip rule in place, even if it initially appears to be wasteful. I'm usually not one to promote inclusion of unnecessary code, but this will possibly make a huge difference in the success of long-term maintenance of this code.

The main drawback to this method is that the "skip counts" in the rules with [S=nn] on them must be rigorously maintained if the number of skipped rules is changed.

Note also that the skip rules' RewriteCond patterns could be simplified if this entire section of code were preceded by (or included) one or more redirect rules to properly canonicalize the requested hostname. The provision to optionally match "www." and the [NC] (No Case) flag could be removed from both rules' RewriteConds. However, be careful of rule order in order to avoid multiple "chained" or "stacked" redirects. See the thread in our Apache Forum Library on proper RewriteRule and directive ordering [webmasterworld.com].

Further, note the use of the [L] flag on all rules where not implicit or precluded by logic requirements; This is potentially critical to proper operation here, as well as a good general practice.

Jim

ensteve




msg:4239917
 1:08 am on Dec 8, 2010 (gmt 0)

Hi Jim,

Thanks for the excellent explanation - your solution worked perfectly.

Cheers,
Sam

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved