简体   繁体   中英

htaccess rewrite url with multiple parameters in a sub folder

I need to rewrite the following url:

 http://localhost/homemarket/products/C1/C2/

to

http://localhost/homemarket/products.php?catergory=C1&sub_category=C2

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 .php 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
</Files>

#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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM