2014-10-27 62 views
1

我想知道如何使用mod rewrite将不具有尾部斜杠的第一级子文件夹重写为php文件。重写没有结尾斜杠的第一级子文件夹htaccess

例如

/test  -> rewrites to file.php?id=test 
/test/  -> trailing slash so it follows through as normal to /test/ 
/test/test -> second level sub folder so it follows through as normal to /test/test 
/  -> homepage needs to follow through as usual to index.php 

试图想一个规则,1号线要做到这一点的。对于上面的例子,测试只是一个例子,规则应该重写任何字符串。

回答

1

您可以在DOCUMENT_ROOT/.htaccess文件中使用此代码:

RewriteEngine On 
RewriteBase/

RewriteRule ^([^/.]+)$ file.php?id=$1 [L,QSA] 
1

只能通过禁用mod_dir的DirectorySlash来完成此操作。有很多理由要求在目录上使用尾部斜线,否则即使存在索引文件,也可以看到整个内容。所以这是令人难以置信的风险。

DirectorySlash Off 

RewriteEngine On 

# attempt to add trailing slashes 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^([^/]+)/(.+[^/])$ /$1/$2/ [L,R] 

# route to file.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^/]+)$ /file.php?id=$1 [L] 
相关问题