Perform If statements for a specific file name in

2019-07-23 05:56发布

问题:

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.