【发布时间】:2015-11-23 12:22:31
【问题描述】:
我有一个关于 htaccess 的问题要问
我有这个结构
book
-> read.php
-> avatar
--> 1
--> index.html
所以在过去,我使用 read.php 来提供我的内容
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ read.php?id=$1&num=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ read.php?id=$1&num=$2&page=$3 [L]
但是现在,我有了自己的文件夹结构和预生成的 html,所以它们驻留在
avatar/1/index.html
但是当我输入例如
avatar/1/2 这是一个不存在的文件夹和文件,我想使用我的 read.php 而不是它尝试访问返回 403 的文件和文件夹。
如果文件夹不包含 index.html,我如何更改我的 htaccess 代码以使用 read.php 及其查询字符串
谢谢
例如,如果我输入
/book/avatar/2
由于没有 /book/avatar/2/index.html (目前它给我扔了 403 禁止)
它应该使用 htaccess
book/read.php?id=$1&num=$2&page=$3
-- 更新 htaccess
RewriteEngine on
RewriteBase /book
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)/(\d+)/([^/]+)$ read.php?id=$1&num=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/(\d+)/?$ read.php?id=$1&num=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ chapter.php?id=$1 [L,QSA]
如果我输入
example.com/book/avatar/2/1
它仍然加载 chapter.php?id=1
如果我输入
example.com/book/avatar
它还加载 chapter.php?id=1
如果我在 RewriteCond 之后输入这个
RewriteRule ^ - [L]
所有文件都会出现404错误
【问题讨论】:
-
请检查权限,因为“403”意味着一些 acl 或限制,htaccess 对我来说似乎不错。
标签: php regex apache .htaccess mod-rewrite