简单的重写规则在 Apache 服务器上不起作用

Simple rewrite rule is not working on Apache server

当试图在我的服务器上创建 URL 重写规则时,我 运行 遇到了一些问题,所以想测试它是否在一个更简单的情况下工作: URL example.com/test 应该重写为 example.com/index.php,我网站上存在的真实页面。

这是我的 .htaccess 文件的全部内容:

AcceptPathInfo Off

SetEnv PHP_VER 5_3
SetEnv REGISTER_GLOBALS 0

RewriteEngine On
RewriteRule   test.php   index.php

而当我输入 URL example.com/test.php 时的结果:

404 Not Found: The requested URL was not found on this server.

我使 .htaccess 文档针对此测试比平时稍微简单一些。通常,我在文档中还有以下规则:

# Redirect from non-www to www

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]


# Redirect from HTTP to HTTPS

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

这些规则一直都在发挥应有的作用。我的测试重写规则有问题吗?如果没有,为什么重写规则对 www 和 HTTPS 重定向有效?

这是我服务器上文件的结构:

| .htaccess
| www
| -- index.php

使用您显示的示例,请尝试遵循 htaccess 规则文件。将您的 https 和 www 实施规则放在文件顶部。

确保您的 htaccess 和 index.php 文件位于根目录中。 请确保在测试您的 URL 之前清除您的浏览器缓存。

RewriteEngine On

# Redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule  ^test/?$   index.php [NC,L]

除了 htaccess 中的一些小的其他更改外,使用您显示的示例更改 htaccess 的结构如下(例如用于重定向的固定正则表达式、在重定向中使用 NE 标志、https 和 www 重定向的组合规则等;改进你的规则文件)

| .htaccess
| www
| -- index.php
| -- .htaccess (new)