Forum Moderators: phranque
<IfModule mod_rewrite.c>
Options FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI}!(^/path/.*(gif¦php¦html¦css¦jpg¦png¦js¦asf¦avi¦wmv¦swf¦xsl¦jar)) [NC]
RewriteRule ^path/(.+) /index.php?q=$1 [L]
</IfModule>
This works correctly if you were to do something like this:
http://example.com/path/webmasterworld.com
or
http://example.com/path/foo.jpg
But I want it to also allow a url like:
http://example.com/path/webmasterworld.com/foo.jpg
As of now that URL would be caught in the condition.
I was thinking that I could look for only the first instance of a period in the URI and then analyze that to see if it's a file extension or a domain extension but I'm not sure how to write that.
Thanks in advance.
This modified RewriteCond excludes only URL-paths containing a single period preceding the filetype. Your URL_paths which include domain names, and those which do not include one of the specified filetypes will not be excluded, and therefore will be rewritten and passed to your script.
RewriteCond %{REQUEST_URI} !^/path/[b][^.]+\.([/b]gif¦php¦html¦css¦jpg¦png¦js¦asf¦avi¦wmv¦swf¦xsl¦jar[b])$ [/b] [NC]
RewriteRule ^path/(.+)$ /index.php?q=$1 [L]
Jim
RewriteCond %{REQUEST_URI} !^/path/([^.]+\.)+(gif¦php¦html¦css¦jpg¦png¦js¦asf¦avi¦wmv¦swf¦xsl¦jar)$ [NC]
RewriteRule ^path/(.+)$ /index.php?q=$1 [L]
However, the pattern is again optimized to allow matching in only a few passes, as opposed to the original pattern, which would have required many back-off-and-retrys to get a match.
You can shorten the list of filetypes as you like.
Jim
[edited by: jdMorgan at 7:25 pm (utc) on May 24, 2007]
1. mysite.com/path/example.jpg
This should NOT be redirected
This works with my condition but not your's
2. mysite.com/path/webmasterworld.com
This should be redirected
This works with both your condition and mine
3. mysite.com/path/webmasterworld.com/example.jpg
This should be redirected
This works with your condition but not mine
if possible I'd need to deal with subdomains too, so:
4. mysite.com/path/subdomain.webmasterworld.com
This should be redirected
5. mysite.com/path/subdomain.webmasterworld.com/example.jpg
This should be redirected
Is this helping at all or am I making things worse?
Thanks for your patience.
RewriteCond %{REQUEST_URI} !^/path/[^.]+\.(gif¦php¦html¦css¦jpg¦png¦js¦asf¦avi¦wmv¦swf¦xsl¦jar)$ [NC]
RewriteRule ^path/(.+)$ /index.php?q=$1 [L]
> 1. mysite.com/path/example.jpg
> This should NOT be [rewritten]
Because the filetype ".jpg" is not preceded by any additional periods, this URL-path matches the pattern, the match is negated by "!", and therefore the rule *is not* applied, and no rewrite takes place.
> 2. mysite.com/path/webmasterworld.com
> This should be [rewritten]
Because there is no matching filetype, this URL-path does not match the pattern, the non-match is negated by "!", and therefore the rule *is* applied, and the URL is rewritten.
> 3. mysite.com/path/webmasterworld.com/example.jpg
> This should be [rewritten]
Because there is an additional period preceding the ".jpg" filetype, this URL path does not match the pattern, the non-match is negated by "!", and therefore, the rule *is* applied and the URL is rewritten.
By way of explanation, the pattern "!^/path/[^.]+\.(gif¦php¦html¦css¦jpg¦png¦js¦asf¦avi¦wmv¦swf¦xsl¦jar)$" means "match a URL-path starting with '/path/' followed by one or more characters not a period, followed by a period, and ending with one of 'gif', ..., or 'jar', and then invert the match/no-match result."
This pattern will not match any URL-path not starting with "/path/", containing more than one period, or not ending with one of the specified filetypes, and then the "!" NOT operator reverses this match/no-match result.
So that's why I asked if you were completely flushing your browser cache before testing the new code: Doing a Reload or closing and restarting the browser is not sufficient -- You must go into the browser options and explicitly empty the cache.
Also, make sure you are replacing the broken pipe "¦" characters in the patterns with solid pipes before use; Posting on this forum modifies the pipe characters, the change is easy to overlook, I forgot to mention this above, and this may in fact be the cause of the problem. However, having already typed all of the above, I'll leave it, in case this is not the problem.
We will discuss subdomains later; Although the rule above appears to meet this requirement as well, one must never add complexity to a simpler but unsolved problem... :)
Jim