mod_rewrite /foo to /index.php?id=foo AND /foo/foo2 to /inde


mod_rewrite /foo to /index.php?id=foo AND /foo/foo2 to /index.php?id=foo/foo2

我有一个问题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的问题?

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

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]