Jimmy Adaro Jimmy Adaro - 6 days ago 5
PHP Question

mod_rewrite with previous rule affecting other rule

I have the next

RewriteRule
for being able of access to

domain.com/section/products/whatever


from the folder called
_products
. The original URL looks like this:

domain.com/section/_products/product.php?param=whatever


This is that rule:

RewriteRule ^section/products/(.*)$ section/_products/$1 [L,NC]





Now I need to rewrite this URL:

domain.com/section/products/brand/index.php?brand=apple


To this one:

domain.com/section/products/brand/apple


I'm trying with this:

Options -Indexes
Options +FollowSymlinks
Options -MultiViews
RewriteEngine on
RewriteBase /

RewriteRule ^section/products/(.*)$ section/_products/$1 [L,NC]

RewriteRule ^(section/products/brand)/([\w-]+)$ /section/_products/brand/index.php?brand=$1 [QSA,L,NC]


Also with this:

RewriteRule ^(section/products/brand)/([\w-]+)$ /section/products/brand/index.php?brand=$1 [QSA,L,NC]


None of those two are working for me.

Should the
section/_products/$1
rule go after or before the other rule?
I can't find the right logic.

Answer

You can have it like this:

Options +FollowSymlinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^(section/products/brand)/([^/]+)$ $1/index.php?brand=$2 [QSA,L,NC]

RewriteRule ^(section)/products/(.*)$ $1/_products/$1 [L,NC,QSA]
Comments