簡體   English   中英

htaccess 在子文件夾中使用多個參數重寫 url

[英]htaccess rewrite url with multiple parameters in a sub folder

我需要重寫以下網址:

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

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

我嘗試在整個 stackoverflow 上進行搜索,並發現了類似的重寫規則,但我面臨以下問題:

  • 這些規則與我的刪除 .php 規則相沖突(將.php添加到我的 sub_category 查詢中)。
  • 如果子類別被刪除,則重定向到 404 頁面。
  • 如果沒有提供它們,則再次重定向到 404 頁面。

這是我嘗試過的:

# 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

您的第一條規則匹配兩個 uri,您需要排除規則中的斜杠,以免與其他規則沖突:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM