Perform If statements for a specific file name in .htaccess

折月煮酒 提交于 2019-12-24 00:46:59

问题


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>

回答1:


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.


来源:https://stackoverflow.com/questions/47440106/perform-if-statements-for-a-specific-file-name-in-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!