Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache rewrite exception to not include specific directories

The Setup

My setup is to have resources shared for two or more sites that have similar structures though different content. In example...

http:// localhost/site1/

http:// localhost/site2/

There are two types of rewrites, CMS content (pretty much just content that is echoed out to the page) and special modules (e.g. blog software module) where I have software that goes in to more specific handling of content from the database.

So the first rewrite rule for the blog makes sure that the blog module handles blog requests....

http:// localhost/site1/blog/*

http:// localhost/site2/blog/*

...using the blog module software located at...

http:// localhost/blog/

The CMS rewrite rule is intended to handle non-specific module requests...

http:// localhost/site1/*

http:// localhost/site2/my_page.html*

...using the CMS rewrite software located at...

http:// localhost/rewrite.php

The Problem

The blog module and CMS module rewrites are conflicting. I've attempted to make an exception using the following rule. Here is my code...

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.js$
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule !.*/(admin|blog|forums)(.+)$ rewrite.php

The last rule doesn't actually work. With this code if I access..

http:// localhost/site1/blog/*

http:// localhost/site2/blog/*

...any blog (or admin or forums) URL for any site is still being rewritten to work with localhost/rewrite.php.

So how do I adjust the last rule to meet the following conditions please...

1.) The first directory (site1 or site2 in localhost/site1/blog) remains dynamic so I can add a third site if I want without having to readjust the code for any reason in that regard.

2.) That the blog (or admin or forums) index (e.g. blog/, forums/, admin/) are handled by their own modules as well as anything inside of those directories (e.g. admin/1, admin/test.html) regardless of HTTP code, 200, 404, etc.

3.) Any URL that is not in the last rule's exception list is handled by rewrite.php (regardless of HTTP code, 200, 404, etc).

4.) localhost/site1/blog/ is not handled by rewrite.php and localhost/site1/random_path is not handled by the blog module rewrite.

I'll be happy to quickly reply with any further clarifications.

like image 358
John Avatar asked Nov 29 '11 12:11

John


1 Answers

Thanks to an earlier RewriteCond someone wrote that made sense I adapted it and it worked great!

Note if someone decides to use this code that the conditions apparently only work AFTER the specific rewrite rules for the modules (admin, blog, forums) though BEFORE the CMS rewrite.php rule.

I'll be happy to honor any positive critiques.

RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(css|js|zip)$

RewriteRule .*/admin(.+) admin$1 [QSA]
RewriteRule .*/blog(.+) blog$1 [QSA]
RewriteRule .*/forums(.+) forums$1 [QSA]

#individual...
RewriteCond %{REQUEST_URI} !.*/admin
RewriteCond %{REQUEST_URI} !.*/blog
RewriteCond %{REQUEST_URI} !.*/forums

#condensed...
RewriteCond %{REQUEST_URI} !.*/(admin|blog|forums)

RewriteRule !\.(css|js|zip)$ rewrite.php
like image 148
John Avatar answered Oct 11 '22 15:10

John