R – Two mod-rewrite rules

mod-rewriteurl-routing

I need two conditional rules. Background:

http://www.domain.com/.htaccess
http://www.domain.com/index.php
http://www.domain.com/admin/index.php

I have both re-write rules:

ROOT:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*/)([^/]+)/([^/]+) $1?$2=$3&%1 [L]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^/]+)/ $1.php?%1 [L]

ADMIN:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]+?)/([a-z]+?)/(.*)$ index.php?model=$1&view=$2¶ms=$3 [L,NS]

I need to somehow have only the one .htaccess file on the root level use both rules but the ADMIN rules only apply when url http://www.domain.com/admin/foo/bar/something/else and the ROOT rule applies to http://www.domain.com/foo/bar/something/else.

Thank you in advance, I have killed an entire day trying to do this and when one works the other gives me a "Not Found" page error.

Kris

Best Answer

I’d use this:

RewriteEngine On

# /admin/ rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/([a-z]+)/([a-z]+)/(.*)$ admin/index.php?model=$1&view=$2&params=$3 [L,NS]
RewriteRule ^admin/ - [L]

# other rules
RewriteRule ^(.*/)([^/]+)/([^/]+)$ $1?$2=$3 [L,QSA]
RewriteRule ^([^/]+)/ $1.php [L,QSA]

The QSA flag (query string append) replace the RewriteCond %{QUERY_STRING} (.*) directives.

Related Topic