WordPress sites at root and at virtual directories

Topics: User Forum
Oct 13, 2008 at 8:51 PM
I am attempting to use IIRF to rewrite URLs for WordPress sites installed at the root and at virtual directories.  Here are some example URLs (minus the http:).

  • //test.dpotter.net/
  • //test.dpotter.net/Technical/
Based on other references here on this site I've been able to get the root to work but not the virtual directories.  It keeps inserting index.php at the beginning of the rewritten URL.  Here is the contents of my current .ini file:

RewriteLog       C:\WebTools\LogFiles\IIRF
RewriteLogLevel  5

RewriteCond     %{URL}                                  ^/Technical/(.*)$       [I]
RewriteRule     ^/Technical/(?!index.php)(?!wp-)(.*)$   /Technical/index.php/$1 [I,L]

RewriteRule     ^/(?!index.php)(?!wp-)(.*)$             /index.php/$1

Here is my goal:

  1. When someone navigates to anything at or below /Technical/ (without the index.php) send them to an url that begins with /Technical/index.php, unless it begins with /Technical/wp-
  2. If the previous two rules fail, then send them to an url that begins with /index.php, unless it begins with /Technical/wp-
  3. If someone specifies the index.php explicitly, I want no rewriting to occur.

What happens instead is that the first rule fails and the second rule is applied.  Here is an excerpt from one of the log files (I set the level to 5 to try to track down this problem):

Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree: getting 'url'
Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree - no joy (GetLastError()=1413)
Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree: 128 bytes
Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree: result ''
Mon Oct 13 12:45:40 2008 - GetHeader_AutoFree: getting 'url'
Mon Oct 13 12:45:40 2008 - GetHeader_AutoFree: 62 bytes
Mon Oct 13 12:45:40 2008 - GetHeader_AutoFree: result '/Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree: getting 'QUERY_STRING'
Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree: 1 bytes
Mon Oct 13 12:45:40 2008 - GetServerVariable_AutoFree: result ''
Mon Oct 13 12:45:40 2008 - GetHeader_AutoFree: getting 'method'
Mon Oct 13 12:45:40 2008 - GetHeader_AutoFree: 4 bytes
Mon Oct 13 12:45:40 2008 - GetHeader_AutoFree: result 'GET'
Mon Oct 13 12:45:40 2008 - DoRewrites: New Url: '/Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - ApplyRules: depth=0
Mon Oct 13 12:45:40 2008 - ApplyRules: Rule 1 : -1 (No match)
Mon Oct 13 12:45:40 2008 - ApplyRules: Rule 2 : 2 matches
Mon Oct 13 12:45:40 2008 - ReplaceServerVariables: InputString='/index.php/$1' out='/index.php/$1'
Mon Oct 13 12:45:40 2008 - GenerateReplacementString: src='/Technical/wp-content/themes/default/images/kubrickbgwide.jpg','(null)' ReplacePattern='/index.php/$1' vec=[[  0, 61, 1, 61] [] ] counts=2,0
Mon Oct 13 12:45:40 2008 - GenerateReplacementString: replacing ($1) with 'Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - ApplyCaseConversion: before '/index.php/Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - ApplyCaseConversion: after  '/index.php/Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - GenerateReplacementString: result '/index.php/Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - ApplyRules: Result (length 71): /index.php/Technical/wp-content/themes/default/images/kubrickbgwide.jpg
Mon Oct 13 12:45:40 2008 - ApplyRules: depth=1
Mon Oct 13 12:45:40 2008 - ApplyRules: Rule 1 : -1 (No match)
Mon Oct 13 12:45:40 2008 - ApplyRules: Rule 2 : -1 (No match)
Mon Oct 13 12:45:40 2008 - ApplyRules: returning 0
Mon Oct 13 12:45:40 2008 - ApplyRules: returning 1
Mon Oct 13 12:45:40 2008 - DoRewrites: Rewrite Url to: '/index.php/Technical/wp-content/themes/default/images/kubrickbgwide.jpg'
Mon Oct 13 12:45:40 2008 - DoRewrites: not recording OriginalUrl (0x000ce150)
Mon Oct 13 12:45:40 2008 - DoRewrites: Finished


Looking at the log file, I don't understand why Rule 1 failed.

Thanks for any help you can give me.
David

Coordinator
Oct 14, 2008 at 5:39 AM
I don't know why rule 1 failed either, and I don't know why the filter would be retrieving the QUERY_STRING server variable. 

You didn't post the entire log, including the part where the filter logs the rules as it reads them in.  Could it be that the filter is not reading in the rules you think it is reading.

Next:  The RewriteCond on the first rule is completely redundant with the actual pattern on the rule.  you don't need the RewriteCond, as you have it written now.  The RewriteRule is doing a pattern match on the URL, so any time you have a RewriteCond and it is matching against the %{URL} Server variable, you could do the same thing in a bare RewriteRule.

I'd advise you to check out the testdriver.exe tool - it is designed to support what you are doing right now.
You can specify a bunch of incoming URLs and test your rules against them in a batch.  It makes for much simpler edit-test cycle on rewrite rules.
Try it.
It is included in the binary release of  IIRF.

Oct 14, 2008 at 11:20 AM
Thanks for the reply.  I had prepared a very long reply with a larger section of my log file but then started trying different things and stumbled on the answer.  From there I've been able to construct a rewrite file that does what I want it to do.  As an aside, I'd tried the TestDriver tool and the sample URLs failed to match with it but I didn't know why at the time.

Now, I figure I should share my rewrite file with the community :-).  I'm doing the following things in my rewrite file:

  1. Removing index.php from the visible URL (i.e. redirect to the one without the index.php in it).
  2. Changing my permalink structure to remove the day (i.e. from /2008/10/01/whatever to /2008/10/whatever).
  3. Supporting multiple virtual directories.
  4. Finally adding in the index.php so that IIS knows what to do.
Here's my INI file with comments.

# Remove the day and (optionally) index.php from the URL.
# Old style --> /Technical/index.php/2008/10/01
# New style --> /Technical/2008/10
RewriteRule ^/(Technical|David)/(index.php/)*([1-2][0-9]{3})/([0-1][0-9])/([0-3][0-9])(.*)$ /$1/$3/$4$6 [I,R=301]

# Remove index.php and redirect to the URL without it.
# Old style --> /Technical/index.php/whatever
# New style --> /Technical/whatever
RewriteRule ^/(Technical|David)/index.php(.*)$  /$1/$2  [I,R=301]

# Add the index.php back in since IIS requires it, but don't show it to the user.
# Only rewrite if the following conditions are true:
#   --> There is no index.php right after the virtual directory name.
#   --> The address does not contain  one of the wp-* names in it.
# Stop processing rules if this rule matches.
# That avoids having the next rule adding index.php before the virtual directory name.
RewriteRule ^/(Technical|David)/(?!index.php)(?!wp-)(.*)$ /$1/index.php/$2 [I,L]

# Rewrite URLs for the root that don't have index.php in them.
# Only rewrite them if they aren't for a file or directory that exists.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule                     ^/(?!index.php)(?!wp-)(.*)$     /index.php/$1
The one thing I don't like with my solution is that there are three lines I have to edit if I want to add or remove a virtual directory.

I've done a bunch of testing, although I'd say that it is far from extensive.  Let me know if you see any issues or if you see a way to simplify.

Thanks for your help.
David
Coordinator
Oct 14, 2008 at 3:09 PM
Really great that you shared that.
The rules look good to me. . .