1

对于我们目前正在处理的网站,我有以下目录结构:

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 会以这种方式运行,我该如何解决?

4

1 回答 1

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行。

于 2013-10-24T13:37:15.683 回答