在HTACCESS重写规则中使用IF语句

时间:2015-11-25 08:02:31

标签: .htaccess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]

以上我在我的htacces文件中将directory/page重写为index.php?id=directory/page

这很好。

我还希望能够添加以下内容:

domain.com/sections/page重写为index.php?section=1&id=page

domain.com/sections/page2重写为index.php?section=1&id=page2

domain.com/page重写为index.php?id=page

每个页面的ID会有所不同

1 个答案:

答案 0 :(得分:2)

您必须查看RewriteCondRewriteRule指令。

根据您的修改,这是一个示例.htaccess

RewriteEngine On

# This will process the /sections/(.*) requests, ?section=1 will be appended to query string    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^sections\/(.*)?$ index.php?section=1&id=$1 [L,QSA]

# This will process the other requests, as it does now.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-/_]+)/?$ index.php?id=$1 [L,QSA]