apache - 为什么 '/' 是 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”)匹配。我错过了什么?为什么重写试图匹配'/'?
解决方案
推荐阅读
- windows - Janusy.com 或 Janusus.net 网站和论坛
- pine-script - 回测策略时 pine 编辑器中没有数据错误
- c# - 确定 windows 操作系统版本详细信息
- ffmpeg - 将视频从 NTSC 转换为 PAL
- redis - 在 Magento 命令中获取 Redis 移动消息
- python - 如果 Python 同时有多种编码方法怎么办?
- reactjs - React fontawesome 删除类名
- javascript - 根据窗口大小调整 iframe 内容的布局
- c++ - 尝试进行 Dll 注入 谁能帮我解决这个错误?
- sql - 逐行遍历 Oracle SQL 查询结果,并产生子查询 - 运行效率不高