在.htaccess中为特定文件名执行If语句

时间:2017-11-22 16:55:20

标签: regex apache .htaccess mod-rewrite url-rewriting

我有一个重写规则,但我希望重写规则只在页面不是index.php时运行,我尝试过使用If语句,但都无效。我的代码,我尝试过的。

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^index$ ./index.php // i did a URL rewrite for removing the .php 

<If "%{HTTP_HOST } == 'index'">
// do nothing here else 
</If>
<Else> //run the code in the else statement.
RewriteRule ^ profile.php [NC,L]
RewriteRule ^profile/([0-9a-zA-Z]+) profile.php?id=$1 [NC,L]

RewriteRule ^ zprofile.php [NC,L]
RewriteRule ^zprofile/([0-9a-zA-Z]+) zprofile.php?id=$1 [NC,L]
</Else>

1 个答案:

答案 0 :(得分:1)

我在缺乏光彩之夜之后解决了这个问题,下面是这个简单的代码,但是我用另一种方法做了,而不是使用if语句,我只是重写了我知道我的目录在哪里的页面,所以任何页面都没有存在于我的目录中应该被重定向到特定的页面,其中url是相同的。

 Options +FollowSymLinks

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f


RewriteRule ^zindex$ zindex.php //file in the directory

RewriteRule ^index$ index.php //file in the directory

RewriteRule ^zprofile$ zprofile.php //file in the directory

RewriteRule ^logout logout.php [NC] //file in the directory


RewriteCond %{REQUEST_FILENAME} !-f //if file does not exist in directory redirect
RewriteCond %{REQUEST_FILENAME} !-d //if file does not exist in directory redirect
RewriteRule ^.*$ profile.php [L] //to profile.php page with the URL still being the same.