我目前正在处理的网站有以下目录结构:
root
- admin/
- assets/
- cache/
- images/
- .htaccess
- category.php
- image.php
- resize.php
我想根据一些条件重写URL,并使用.htaccess
实现了这一点。出于某种原因,Apache似乎完全忽略了我添加的RewriteCond
规则。这是.htaccess
文件的副本:
DirectoryIndex index.php category.php
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Image rewrites:
RewriteRule ^([^/]+)/([0-9]+)/(.*)$ resize.php?function=resizeRatio&width=$2&src=images/$1/$3 [L]
RewriteRule ^([^/]+)/([^/]+)([0-9]+)/(.*)$ resize.php?function=resizeRatio&width=$3&src=images/$1/$2/$4 [L]
RewriteRule ^([^/]+)/([0-9]+)x([0-9]+)/(.*)$ resize.php?function=resizeCrop&width=$2&height=$3&src=images/$1/$4 [L]
RewriteRule ^([^/]+)/([^/]+)([0-9]+)x([0-9]+)/(.*)$ resize.php?function=resizeCrop&width=$3&height=$4&src=images/$1/$2/$5 [L]
# Category rewrites:
RewriteRule ^([^/]+)/$ category.php?parent=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ category.php?parent=$1&child=$2 [L]
RewriteRule ^([^/]+)/([^/]+).(.*)$ image.php?image=$2.$3&parent=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+).(.*)$ image.php?image=$3.$4&parent=$1&child=$2 [L]
尝试访问/admin/
时,我会被转到category.php?category=admin/
的重写页面。
为什么Apache会以这种方式运行,我该如何解决它?
答案 0 :(得分:1)
RewriteCond
仅适用于下一个RewriteRule
。
将RewriteCond
行替换为:
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
这将避免有效文件,目录或链接的其余RewriteRule
行。