.htaccess - 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§ion=products [QSA,L]
RewriteRule ^([a-z0-9_\-\.\(\)]+)/([a-z0-9_\-\.\(\)]+)?$ index.php?lang=en&category=$1&category_lvl2=$2§ion=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§ion=products [QSA,L]
解决方案
以下列方式保存您的 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§ion=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§ion=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§ion=products [QSA,L]
推荐阅读
- gitlab-ci - Gitlab Pipeline 使用 Azure 镜像如何访问变量?
- java - 用文件填充数组
- ssl - 创建 cUrl 请求时 SSL 证书已过期。域-> subdomain.domain (Magento)
- c++ - 在 Windows 上使用 Qt+ICC
- oracle-apex - Oracle APEX 预渲染点 - APEX 21.1
- java - 根据 kotlin 中的时间戳过滤 json 响应
- javascript - 使用 .map() 反应的未定义变量
- ios - 我的应用程序图标未正确加载?
- python - 由于 EnvironmentError 无法安装软件包:[WinError 5] 访问被拒绝
- json - 如何设置从 Power BI 到 Google Firebase 的连接?