【问题标题】:Perform If statements for a specific file name in .htaccess对 .htaccess 中的特定文件名执行 If 语句
【发布时间】:2017-11-22 16:55:19
【问题描述】:

我有一个重写规则,但我希望重写规则仅在页面不是 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>

【问题讨论】:

  • index更改为实际网址。
  • @Lag 响应服务器错误。
  • @Chris85 谢谢,但我真的不明白,请你在下面给出一个例子作为可接受的答案。
  • @chris85:不,因为%{HTTP_HOST} 永远不可能是index。它的价值为ww.example.com
  • @anubhava 啊,是的,这是有道理的。

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


【解决方案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.

【讨论】:

    猜你喜欢
    • 2018-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2021-04-14
    • 2016-03-03
    相关资源
    最近更新 更多