首页 > 解决方案 > 为什么 '/' 是 Apache Rewrite 评估的第一个 uri?

问题描述

我目前正在使用 Windows 10/Apache2.4/PHP 7.3 开发服务器并访问此 url(即,根服务器是 localhost 并且“主机名”通过“主机”文件映射):hostname/page.php?m=150

我想使用 apache 重写命令将具有此模式的任何 url 重定向到,hostname/homepage.php但日志文件显示我的命令没有按照我期望的方式进行评估。

仅供参考,该站点由<VirtualHost>httpd-vhosts.conf 中的一个块配置,因此这是我的重写命令所在的位置。

我已将虚拟主机块剥离为:

<VirtualHost *:80>
    DocumentRoot "C:/Apache24/htdocs/{sitename}"
    ServerName {sitename}
    ServerAlias {sitename}
    
    Options -FollowSymLinks -Indexes -MultiViews
    
    RewriteEngine On

    LogLevel alert rewrite:trace6

    #Just testing to see if the command block is found
    #Note: The log file reports this as a match.
    RewriteCond %{HTTP_HOST} sitename 

    #part 1: IF the URI starts with /page.php...
    #the show stops here...Apache finds no match, with or without the '/'.
    #even though page.php is entered into the browser, it is never evaluated
    RewriteCond %{REQUEST_URI} ^/page.php

    #Part 2: AND there's a query string that matches this pattern  
    #this condition is never reached since Part 1 fails
    RewriteCond %{QUERY_STRING} ^m=[0-9]{1,3}$ 

    #THEN: rewrite the url and go to /homepage.php (forgetting the query)
    RewriteRule (^.$) /homepage.php [R=301,QSD,END]
    
    ErrorLog "logs/sitename_error.log"
    
</VirtualHost>

当我放入hostname/page.php?m=150浏览器时,该{REQUEST_URI}行无法匹配。当我查看 siteroot_error.log 时,日志中的前 3 个条目如下所示(删除了不可读的数据):

.... init rewrite engine with requested uri /  #WHY / and not /page.php????
.... applying pattern '(^.$)' to uri '/'
.... RewriteCond: input='hostname' pattern='hostname' => matched #Duh!
.... RewriteCond: input='/' pattern='/page.php' => not-matched
.... pass through /
.... init rewrite engine with requested uri /index.php
.... RewriteCond: input='' pattern='^m=[0-9]{1,3}$' => not-matched

日志中的条目正在评估请求“/”。它不应该评估 '/page.php' 吗?无论如何,它找不到匹配项,因此传递“/”。因此,apache 会附加“index.php”,因为这是服务器 apache 配置文件中 DirectoryIndex 参数中设置的第一个文件。并且,在这种情况下,'/' 目录中没有 index.php 或 index.html 文件,因此请求失败。

在我看来,应该出现在日志文件中的第一个评估是对“/page.php”的评估,而 apache 应该尝试将其与模式(“/page.php”)匹配。我错过了什么?为什么重写试图匹配'/'?

标签: apachemod-rewritevhosts

解决方案


推荐阅读