問題描述
多個相同的 url htaccess 重定向 (Multiple same url htaccess redirect)
I created a custom product/ecommerce website. It has a category, sub‑category and product. Here are the urls that I want to redirect:
website.com/clothes
website.com/clothes/
website.com/clothes/tshirts
website.com/clothes/tshirts/
website.com/clothes/tshirts/bluepinned
website.com/clothes/tshirts/bluepinned/
Here are the .htaccess entries. Is there a way to write the following in one line? For example if the URL was, website.com/clothes/tshirts making sure the PHP files realise that I want to load the tshirts category, and not to display a product called tshirts in the clothes categor.
RewriteCond %{REQUEST_FILENAME} !‑d
RewriteCond %{REQUEST_FILENAME} !‑f
RewriteRule ^(clothes)/([^/\.]+)/([^/\.]+)/?$ products.php?c=$1&sc=$2&p=$3 [NC,L,QSA]
RewriteRule ^(clothes)/([^/\.]+)/?$ products.php?c=$1&sc=$2 [NC,L,QSA]
RewriteRule ^(clothes)/?$ products.php?c=$1 [NC,L,QSA]
I sometimes get 406 errors too when loading product images ‑ are there any rewrite rules that could be making this happen. The actual images are in a separate directory anyway website.com/images/clothes/tshirts/bluepinned.jpg
‑‑‑‑‑
參考解法
方法 1:
You need to apply the conditions to each of the rules separately:
RewriteCond %{REQUEST_FILENAME} !‑d
RewriteCond %{REQUEST_FILENAME} !‑f
RewriteRule ^(clothes)/([^/\.]+)/([^/\.]+)/?$ products.php?c=$1&sc=$2&p=$3 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !‑d
RewriteCond %{REQUEST_FILENAME} !‑f
RewriteRule ^(clothes)/([^/\.]+)/?$ products.php?c=$1&sc=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !‑d
RewriteCond %{REQUEST_FILENAME} !‑f
RewriteRule ^(clothes)/?$ products.php?c=$1 [NC,L,QSA]
It's also possible that there is a relative‑vs‑absolute path issue that could possibly be fixed by adding a
<base href="/">
in your page headers.