当前位置: 动力学知识库 > 问答 > 编程问答 >

php - Regarding HTaccess and sub directory

问题描述:

I got a question to ask regarding htaccess

I have this structure

book

-> read.php

-> avatar

--> 1

--> index.html

So in the past, I use read.php to serve my content

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]

However now, I got my own folder structure with the pre-generated html so they reside in

avatar/1/index.html

But when I type e.g

avatar/1/2 which is a non existent fodler and file, I wanna use my read.php instead of it trying access the file and folder which return 403.

How can I change my htaccess code to use the read.php with its query string if the folder does not contain index.html

Thanks

E.g if I type

/book/avatar/2

Since there is no /book/avatar/2/index.html (currently it throw me 403 forbidden)

it should use the htaccess

book/read.php?id=$1&num=$2&page=$3

-- Updated 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]

If i type

example.com/book/avatar/2/1

It still load chapter.php?id=1

If i type

example.com/book/avatar

it also load chapter.php?id=1

If i key in this after RewriteCond

RewriteRule ^ - [L]

All files will 404 error

网友答案:

Inside /book/.htaccess you can create this rule:

RewriteEngine On
RewriteBase /book/

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

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]

This will handle /book/avatar/2 OR /book/avatar only if request is not a file or directory.

分享给朋友:
您可能感兴趣的文章:
随机阅读: