homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Mod Rewrite modification
Need it to be conditional on the domain name

 3:37 am on Jan 17, 2009 (gmt 0)

I'm trying to get started with modifying some rules to serve cache files on a customised wordpress install.

I have set the server up to have multiple blogs running from one install, each of which have their own directory under "content" for things like themes and plugins.

The cache wants a directory called "cache" in the "content" directory, but I'd like to redirect that th "content/EXAMPLE/cache".

The "EXAMPLE" part can be derived, as it will always be the domain name, minus the www. and the .com/.org etc.

Here's the exising code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
AddDefaultCharset UTF-8
RewriteCond %{REQUEST_URI} !^.*[^/]$
RewriteCond %{REQUEST_URI} !^.*//.*$
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{HTTP:Cookie} !^.*(comment_author_安ordpress安p-postpass_).*$
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{DOCUMENT_ROOT}/content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz -f
RewriteRule ^(.*) /content/cache/supercache/%{HTTP_HOST}/$1/index.html.gz [L]

So, either I set it up to derive the "EXAMPLE" folder automatically, or if that isn't possible, would it be reasonable to add a specific set of rules for each domain?

Such as:
example.com/content/cache -> example.com/content/example/cache
example2.com/content/cache -> example2.com/content/example2/cache

I hope this is clear! Many thanks in advance,




 10:43 am on Jan 17, 2009 (gmt 0)

RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.(com她rg好et) [NC]

will match the domain name, even when requested without www and put it in to %2.

You can't just drop this line into your ruleset and hope it will work. The %2 can only be used by the line of code that immediately follows it.

At present you are collecting a %1 from the line immediately preceding the RewriteRule and so you will need to find a way to "transport" the value through two lines to reach the RewriteRule and have it still available.


If there are a LOT of TLDs to match, and any of them are like .co.uk or .info (i.e. NOT three letters) then that part of the rule can be tweaked to accept one or two "words" of between two and perhaps seven or eight letters as "wildcard" values, instead of the fixed (com她rg好et) list.

Additionally, are you sure you want a redirect? That will force the browser to make a new HTTP request for a new URL. Perhaps you need an internal rewrite - user sees the same URL, but content is fetched from a different folder inside the server. The location of that folder is not revealed to the user.


 7:06 pm on Jan 17, 2009 (gmt 0)

Here is how I'd code that. I removed a whole bunch of unnecessary "^.*" and ".*$" anchors and subpatterns, simplifying (and speeding up) the regex, and also implemented g1smd's suggestion for extracting the requested hostname. Note however that as I coded it, uppercase or mixed-case variations on the hostname's www subdomain or TLD won't be accepted since this would result in rewriting to different subdirectories based on case, and must therefore be taken care of by a preceding domain-canonicalization redirect rule.

AddDefaultCharset UTF-8
RewriteEngine on
# RewriteBase / -- commented-out: "/" is the default, so this line is not needed
RewriteCond %{REQUEST_URI} /$
RewriteCond %{REQUEST_URI} !//
RewriteCond %{QUERY_STRING} ![=]
RewriteCond %{HTTP:Cookie} !(comment_author_安ordpress安p-postpass_)
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+)\.(com她rg好et)
RewriteCond %{DOCUMENT_ROOT}/content/cache/supercache/%2/$1/index.html.gz -f
RewriteRule (.*) /content/cache/supercache/%2/$1/index.html.gz [L]

Replace the broken pipe "" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Note that if you have different sites on "example.com" and "example.net" then the TLD *must* be included in the domain-subdirectory path and the rewrite, so the RewriteCond pattern would need to be changed to:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]+\.(com她rg好et))
so that the TLD is included in the %2 back-reference.

You don't need the <IfModule> container unless you require or desire this code to fail silently on servers where mod_rewrite support is not installed.



 11:20 pm on Jan 17, 2009 (gmt 0)

Thank you for the replies! I appreciate it. I'll get going with your suggestions.

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