对.htaccess中的特定文件名执行If语句 [英] Perform If statements for a specific file name in .htaccess

查看:72
本文介绍了对.htaccess中的特定文件名执行If语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Rewrite rule but i want the rewrite rule to only run when the page is not index.php, I have tried using If statement, all but no avail. My code, what I have tried.

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>


推荐答案

我在缺乏光泽的夜晚解决了这个问题,下面的简单代码,但是我使用了另一种方法,而不是使用if语句,我只是重写了我首先知道目录中位置的页面,因此目录中不存在的任何页面都应使用url重定向到特定页面

I solved it after a lack lustre night, with this simple code below, but I did it with another approach, instead of use if statement, i simply rewrote the pages i knew where on my directory first so any page that does not exist on my directory should be redirected to a particular page with the url being the same.

 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.

这篇关于对.htaccess中的特定文件名执行If语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆