1

我对 mod_rewrite 有疑问。

我想重定向标题中的那些 URI。我使用以下规则

RewriteEngine on

RewriteCond $1 !^(folders not to be redirected e.g. css|images)
RewriteCond $1 !^(.*).(file extension e.g. png|css|txt|php)

RewriteRule ^(.*)$ index.php?id=$1 [L]

仅当我将所有资源放在一个文件夹中时它才有效,否则它会告诉我:

"/foo/index.php" not found.

所以为了解决这个问题,我将所有资源放在“www”文件夹中

但是当我尝试从例如“foo”的子文件夹加载资源时,它告诉我:

The requested URL "/foo/foo2" was not found on this server.

如何从“/foo/foo2”甚至“/foo/foo2/foo3”之类的子文件夹加载资源?

以及如何解决在文件夹中自动搜索 index.php 的问题?

4

1 回答 1

1

我相信您可以使用以下方法来达到您想要的结果。它不按文件扩展名过滤,而是检查文件是否实际存在。有一点不同的是,它首先在您的链接上附加一个斜杠,这是您可能不想要的。

RewriteEngine on
RewriteBase /

# This appends a trailing slash. You will have to update http://so with your domain.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://so/$1/ [L,R=301]

# This does the internal redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /index.php?id=$1 [L]

如果你不想要尾斜线,你可以使用以下

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?id=$1 [L]
于 2013-04-28T15:44:04.610 回答