4

任何人都可以帮助进行 .htaccess 优化吗?其实我有下一个代码:

RewriteEngine on
RewriteBase /
DirectoryIndex frontend/www/index.php

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_URI} cpanel/(.*)$
# Why not "-f" - because it's domain of 3d level: VirtualDocumentRoot /path/to/htdocs/%-3/ and if I use "-f" file not founds :(
RewriteCond backend/www/%1 -F
RewriteRule . backend/www/%1 [L]

RewriteCond %{REQUEST_URI} cpanel/(.*)$
RewriteCond backend/www/%1 !-F
RewriteRule . backend/www/index.php?b=%1 [L]

RewriteCond %{REQUEST_URI} !cpanel/(.*)$
RewriteCond frontend/www%{REQUEST_URI} -F
RewriteRule . frontend/www%{REQUEST_URI}

RewriteCond %{REQUEST_URI} !cpanel/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . frontend/www/index.php?f=%{REQUEST_URI}

文件夹结构:

.
├── .htaccess (with code I provided)
├── frontend
│   ├── www
│       ├── css
│       │   ├── base.css
│       └── index.php
├── backend
    ├── www
        ├── css
        │   ├── base.css
        └── index.php

我需要:

  • my.domain.com/base.css显示文件是否存在于根目录或 frontend/www 下
  • my.domain.com/dir/base.css显示文件是否存在于 root/dir 目录或 frontend/www/dir 下
  • my.domain.com/cpanel/base.css显示文件是否存在于 backend/www 目录中
  • my.domain.com/cpanel/dir/base.css显示文件是否存在于 backend/www/dir 目录中
  • 如果在任何 nedded 目录中都找不到文件,我会从 frontend/www/index.php 或 backend/www/index.php 显示 index.php(如果 URI 以 cpanel/... 开头)。

看起来我的 .htaccess 工作得很好,但我不确定它的质量。

4

1 回答 1

3

如果你httpd.conf的设置正确,这RewriteBase /是不必要的

RewriteCond %{REQUEST_URI} cpanel/(.*)$部分cpanel/(.*)$应该是^cpanel/(.*)$。没有它,每个包含 '..cpanel/something..' 的 url 都会匹配。

我猜...?f=%{REQUEST_URI}你想将客户端请求的 url 传递给 index.php(可能是记录 404-s)。但是,如果REQUEST_URI包含不需要的字符(例如&),则会导致意外行为,例如,只有直到&的部分REQUEST_URI将作为f参数的值传递,之后的部分&将是另一个新创建的 GET 参数。(请尝试一下。)

您可以REQUEST_URI在 php 中使用$_SERVER['REQUEST_URI']. 如果你省略这部分,你.htaccess会更清楚。

最后两条规则值得一个[L]标志,这并不重要,但是如果你不想链接你的规则,为了安全和可维护性,添加[L]标志总是一个好主意。

我认为您的 .htaccess 除了这些小东西之外还可以。

于 2012-11-19T02:52:38.643 回答