Manikiran Manikiran - 10 months ago 40
PHP Question

htaccess rewrite url with multiple parameters in a sub folder

I need to rewrite the following url:




I tried searching all over stackoverflow, and found similar rewrite rules but I am facing the following problems:

  • These rules clash with my removing .php rules (adds
    to my sub_category query).

  • If subcategory is removed, its redirecting to 404 page.

  • If none of them are provided, its again redirecting to 404 page.

Here's what I have tried:

# Do not remove this line, otherwise mod_rewrite rules will stop working
Options +MultiViews
RewriteEngine On
RewriteBase /

#Prevent viewing of .htaccess file
<Files .htaccess>
order allow,deny
deny from all

#Prevent directory listings
Options All -Indexes

#Error Documents
ErrorDocument 404 /homemarket/error.php?404
ErrorDocument 500 /homemarket/error.php?500

#Remove extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /homemarket/buyers/$1.php [NC,L]
RewriteRule ^products/([^/]*)/([^/]*)/?$ /homemarket/buyers/products.php?category=$1&sub_category=$2 [NC,L]

DirectoryIndex index.php


Your first rule matches both uris, You need to exclude the slash in your rule so that it can not conflict with other rules :

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)$ /homemarket/buyers/$1.php [NC,L]