apache 如果其他类型重写条件和重写规则,是否可以使用 htaccess?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1610576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 18:18:59  来源:igfitidea点击:

Is it possible to have htaccess if else type rewrite conditions and rewrite rules?

apache.htaccessmod-rewriteapache-config

提问by mwalsher

I am trying to write an htaccess file that essentially does this:

我正在尝试编写一个本质上执行此操作的 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]

Is this possible using htaccess?

这可以使用 htaccess 吗?

回答by meder omuraliev

I would think all you need to do is place your 3 unique requested files as 3 RewriteRules at the top, above those RewriteCond's:

我认为您需要做的就是将您的 3 个独特的请求文件作为 3 RewriteRules 放在顶部,高于这些文件RewriteCond

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

// rest of Rewrite Stuff

回答by Gumbo

Try this rule:

试试这个规则:

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

回答by Olivier Pons

This is your solution:

这是您的解决方案:

# if files are know, redirect and stop:
RewriteRule ^(some-file|some-file2|some-file3)\.php$ redirector.php?uri=.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]

Nota: I've removed:

注意:我已删除:

RewriteCond %{REQUEST_URI} ^(.*)$

Which is useless. If you wanted to test "not empty", this should have been:

这是没用的。如果你想测试“非空”,这应该是:

RewriteCond %{REQUEST_URI} !^$

Olivier

奥利维尔