【问题标题】:How to perform If statement in .htaccess if the pages are specific如果页面是特定的,如何在 .htaccess 中执行 If 语句
【发布时间】:2018-05-05 15:09:39
【问题描述】:

当我尝试使用 if 语句在页面特定时运行 RewriteRule 时,我得到了 。我的 apache 服务器是 2.4,它也在我的屏幕截图中。我想在我的代码中做的是

Options +FollowSymLinks
RewriteEngine On

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


RewriteRule ^index$ ./index.php
RewriteRule ^zindex$ ./zindex.php
RewriteRule ^logout$ ./logout.php
RewriteRule ^login$ ./login.php
RewriteRule ^zlogin$ ./zlogin.php
RewriteRule ^404$ ./404.php





#write if statement to check pages that is being loaded else fire for new username

    <If "%{HTTP_HOST} == 'index.php' || %{HTTP_HOST} == 'ajaxsignlog.php' || %{HTTP_HOST} == 'ajaxsignlog1.php' || %{HTTP_HOST} == 'ajaxupload.php' || %{HTTP_HOST} == 'connect.php' || %{HTTP_HOST} == 'logout.php' || %{HTTP_HOST} == 'password.php' || %{HTTP_HOST} == 'top.php' || %{HTTP_HOST} == 'zindex.php' || %{HTTP_HOST} == 'ztop.php'">
                     // do not rewrite rule
    </If>
    <Else> //do some rewrite rules here
    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>

【问题讨论】:

  • HTTP_HOST 将是 domain.com 而不是文件 neam
  • @nogad 谢谢,但我在本地主机上,请你为我举个例子。
  • "REQUEST_FILENAME 与请求匹配的文件或脚本的完整本地文件系统路径,如果在引用 REQUEST_FILENAME 时服务器已经确定了这一点。否则,例如在虚拟主机上下文中使用时, 与 REQUEST_URI 相同的值”httpd.apache.org/docs/2.4/expr.html
  • 可能更容易像这样排除:RewriteCond %{REQUEST_URI} !^/index\.php$
  • 欺骗:stackoverflow.com/questions/14008003/… 和其他几十个人

标签: php apache .htaccess mod-rewrite


【解决方案1】:
RewriteRule ^ profile.php [NC,L]
RewriteRule ^profile/([0-9a-zA-Z]+) profile.php?id=$1 [NC,L]

400 Bad Request 是由 &lt;If&gt;/&lt;Else&gt; 构造中的相对路径替换引起的。出于某种原因,在此上下文中添加回来的 directory-prefix 始终是 *If/(无论该指令是在 &lt;If&gt; 还是 &lt;Else&gt; 块中) - 这很可能执行内部重写时会导致 400 Bad Request(或执行外部重定向时会导致 403 Forbidden)。

而且,在&lt;If&gt;/&lt;Else&gt; 构造中,RewriteRule pattern 匹配绝对文件系统路径,而不是相对于根的 URL 路径。所以,上面的第二个指令永远不会匹配。

因此,如果这些限制/差异影响到您,请不要同时使用 mod_rewrite 和 Apache &lt;If&gt; 表达式。 &lt;If&gt; 表达式之外的指令如何受到影响还存在其他问题。 (就个人而言,如果可能的话,我会避免将两者混为一谈。)

另请参阅my answer 以了解有关 ServerFault 的以下问题,该问题更详细: https://serverfault.com/questions/1054363/bad-request-from-rewriterule-after-putting-if-directive

你不需要&lt;If&gt;/&lt;Else&gt; 表达式来做你想做的事。 mod_rewrite 本身提供了执行此操作的机制,无需其他工具。但是,具体如何实现这一点可能取决于您正在尝试做什么。

在问题的示例中,如果请求与一系列 URL 中的一个匹配,则您希望跳过一系列重写,那么您可以执行以下操作:

RewriteCond %{REQUEST_URI} =/index.php [OR]
RewriteCond %{REQUEST_URI} =/ajaxsignlog.php [OR]
RewriteCond %{REQUEST_URI} =/ajaxsignlog1.php [OR]
RewriteCond %{REQUEST_URI} =/ajaxupload.php [OR]
RewriteCond %{REQUEST_URI} =/connect.php [OR]
RewriteCond %{REQUEST_URI} =/logout.php [OR]
RewriteCond %{REQUEST_URI} =/password.php [OR]
RewriteCond %{REQUEST_URI} =/top.php [OR]
RewriteCond %{REQUEST_URI} =/zindex.php [OR]
RewriteCond %{REQUEST_URI} =/ztop.php
RewriteRule ^ - [S=10]

# 10 Rules follow that are skipped when any of the above URLs are requested
# :
# :

RewriteRule 指令上的 S=10 标志表示当前面的任何 URL 完全匹配时跳过的完整规则的数量。

有关在.htaccess 中实现条件分支的方法的更多想法,另请参阅my answer 到以下问题: Grouping RewriteRule by RewriteCond with .htaccess

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-06
    • 2010-11-12
    • 2011-08-20
    • 1970-01-01
    相关资源
    最近更新 更多