开发者

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

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

if(requested file == "some-file.php" || requested file == "some-file2.php" || requested file == "some-file3.php")
   then Rewrite to redire开发者_开发技巧ctor.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?


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:

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


Try this rule:

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


This is your solution:

# 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]

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜