首页 > 解决方案 > htaccess 文件夹和文件异常不起作用

问题描述

我在 htaccess 中有指令让所有请求都通过 index.php 文件。但它会禁用对文件夹和文件的访问。因此,如果我尝试转到 /uploads/products/thumbs/file.png 它会导致 index.php?lang=en&category=uploads§ion=products (等)而不是从我添加 RewriteCond 的文件夹中获取文件以添加现有的异常文件和文件夹,但由于某种原因它不起作用。

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&section=products [QSA,L]
RewriteRule ^([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&category_lvl2=$2&section=products [QSA,L]
RewriteRule ^([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&category_lvl2=$2&category_lvl3=$3&section=products [QSA,L]

标签: .htaccessmod-rewriteurl-rewritingquery-stringfriendly-url

解决方案


以下列方式保存您的 htaccess 规则文件。请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine On
RewriteBase /
##Conditions with rules for URL 3 levels of slashes here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&category_lvl2=$2&category_lvl3=$3&section=products [QSA,L]

##Conditions with rules for URL 2 levels slashes here.       
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&category_lvl2=$2&section=products [QSA,L]

##Conditions with rules for URL 1 level slash here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&section=products [QSA,L]

推荐阅读