是否可以使用htaccess if else键入重写条件和重写规则?

时间:2009-10-22 23:24:32

标签: apache .htaccess mod-rewrite apache-config

我正在尝试编写一个基本上执行此操作的htaccess文件:

if(requested file == "some-file.php" || requested file == "some-file2.php" || requested file == "some-file3.php")
   then Rewrite to redirector.php?uri=some-file.php <- substitute requested file without passing any parameters
else
// existing rewrite conditions from silverstripe
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]

使用htaccess可以吗?

3 个答案:

答案 0 :(得分:0)

我认为您需要做的就是将3个唯一请求的文件放在顶部3 RewriteRule s,高于RewriteCond的位置:

RewriteEngine On
RewriteRule /(some-file.php) http://test.dev/redirector.php?uri=$1 [L]
RewriteRule /(some-file2.php) http://test.dev/redirector.php?uri=$1 [L]
RewriteRule /(some-file3.php) http://test.dev/redirector.php?uri=$1 [L]

// rest of Rewrite Stuff

答案 1 :(得分:0)

尝试此规则:

RewriteRule ^(some-file\.php|some-file2\.php|some-file3\.php)$ redirector.php?uri=$1

答案 2 :(得分:0)

这是你的解决方案:

# if files are know, redirect and stop:
RewriteRule ^(some-file|some-file2|some-file3)\.php$ redirector.php?uri=$1.php [QSA,L]

# files that were not known go here:
# existing rewrite conditions from silverstripe
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* sapphire/main.php?url=%1&%{QUERY_STRING} [L]

诺塔: 我删除了:

RewriteCond %{REQUEST_URI} ^(.*)$

哪个没用。如果你想测试“非空”,那应该是:

RewriteCond %{REQUEST_URI} !^$

奥利弗