Rewrite rule exception?

Hi,

I have an existing site that uses some sort of mod-rewrite rules that was written by someone long time ago. As far as I remember, it redirects all calls that do not contain dot (.) in URI to site root index.php. The idea was to redirect anything but direct file calls.

OK, so these are the current rewrite rules in .htaccess

RewriteRule ^([^.]*)$ /index.php [L]
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

My task is to integrate a blog (possibly wordpress) into the existing site under the new blog directory. http://www.example.com/blog/
However, I can’t easily use the blog because the rewrite rule always redirects my call to site root index.php.

How cud I add an exception to my rewrite rules that would not trigger the rewrite action when browser being under the blog path?

As an alternative solution. I cud do something like require(‘/blog/index.php’); in my root index file but something tells me this is not the way to go.

:slight_smile:

Thanks,


[COLOR="Red"]RewriteCond %{REQUEST_URI} !^blog[/COLOR]
RewriteRule ^([^.]*)$ /index.php [L]

RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

Try it like this (red line added)

Hi Marek,

First, reverse the order of your mod_rewrite block statements (you want to redirect the domain first).

Then add a RewriteCond statement BEFORE the original first RewriteRule to exclude the blog subdirectory.

Finally, learn some regex ( the ([^.]) atom is almost as bad as (.) as it will capture NOTHING or EVERYTHING (except the dot character in your regex). To me, that’s “lazy regex” and is more likely than not to get you into trouble.

# .htaccess in DocumentRoot

RewriteEngine on

# Force www
RewriteCond %{HTTP_HOST} ^example\\.com [NC]
RewriteRule ^/?(.*)$ http://www.example.com/$1 [R=301,L]

# Redirect extensionless URI in DocumentRoot to index.php
RewriteRule ^/?([-a-zA-Z0-9_]+)$ index.php [L]

# alternatively, redirect extensionless NOTHING/EVERYTHING
# NOT requested of blog/ to index.php
RewriteCond %{REQUEST_URI} !^/?blog/
RewriteRule ^/?([^.]*)$ index.php [L]

BEWARE: WordPress will add mod_rewrite to your .htaccess which SHOULD not interfere with this - just be sure that it follows your mod_rewrite code (and get rid of their <IfModule> enclosure statements - YOU know that mod_rewrite is enabled!).

Regards,

DK