apache - 如何解决 Apache 未应用我的重写规则的问题?

标签 apache tomcat mod-rewrite

我有一个以 Apache httpd 作为前端运行的 tomcat 6 网络应用程序。我正在使用 mod_proxy 和 mod_proxy_ajp 将请求转发给 tomcat。我的服务器正在运行 Ubuntu。现在我正在尝试使用 mod_rewrite 删除前导 www,以便我的规范网站 URL 是 http://example.com 而不是 http://www.example.com

我已经阅读了许多关于使用 mod_rewrite 的教程,但我无法进行任何重写。我尝试将重写规则放入 .htaccess 文件中(在修改我的 /etc/apache/sites-available/default 文件以设置 AllowOverride all 之后)。我尝试将重写规则放入 apache2.confhttpd.confrewrite.conf。我已经在打开重写日志记录的情况下尝试了所有这些。日志文件已创建,但 Apache 未向其写入任何内容。我想也许 mod_proxy 以某种方式阻止使用重写规则,所以我也尝试禁用它......但我仍然没有重写,也没有记录到日志中。

此时我完全不知道接下来要尝试什么。我该如何解决 Apache 不使用我的重写规则的问题?

作为引用,这里是我的重写指令:

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
    RewriteLog "/var/log/apache2/rewrite.log"
    RewriteLogLevel 3

</IfModule>

下面的回答对我的特殊情况很有帮助,但对整个社区的帮助可能不如关于一般如何对 Apache 指令进行故障排除的回答。例如,有没有一种方法可以启用日志记录,以便在收到请求时告诉我哪些指令正在以何种顺序应用?

编辑 2:我现在已经开始工作了。我的虚拟主机设置不正确,而且我也没有完全正确地重写正则表达式。这是我开始工作的最终重写指令:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
        RewriteRule ^(.*)$ http://example.com$1 [L,R=301]
</IfModule>

最佳答案

尝试回答您的问题:要调试 Apache 操作,您可以调整 LogLevel到较低级别(可能是 debug)。但是,即使您放置了 debug 如果您禁用相关模块的日志,您也不会从中收到任何消息。例如,默认的 [RequestLogLevel][2] 为 0,即模块不写入任何消息。我看到你把它设置为 3 但喜欢 RoBorg说将其更改为 9,这对您的情况来说可能太低了。

尝试解决您的问题:尝试使用相反的形式更改重写主机名的方式 - 查看主机名是否是您想要的,如果不是,则将其更改为您想要的主机名.如 URL Rewriting Guide - Apache HTTP Server 中所述在 Apache 站点:

Canonical Hostnames

Description: The goal of this rule is to force the use of a particular hostname, in preference to other hostnames which may be used to reach the same site. For example, if you wish to force the use of www.example.com instead of example.com, you might use a variant of the following recipe. Solution:

# To force the use of 
RewriteEngine On
RewriteCond %{HTTP_HOST}   !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST}   !^$
RewriteRule ^/(.*)         http://www.example.com/$1 [L,R]

在他们的示例中,他们从 example.com 更改为 www.example.com 但您明白了。只需根据您的情况进行调整即可。

关于apache - 如何解决 Apache 未应用我的重写规则的问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/215316/

相关文章:

java - 替换 cookie 值上的逗号字符

mysql - XAMPP 3.2.1 上的 Apache 和 Mysql 未启动

apache - 无效命令 'RedirectMatch' - Apache 2.47。 Ubuntu 信任

node.js - 在tomcat中集成easyrtc服务器

tomcat - jakarta.servlet.ServletException : Class [com. practice.MyServlet] 不是 Servlet

.htaccess - htaccess 文件夹下所有内容的 URL 重写规则

redirect - 创建 IIS URL 重写/重定向

PHP - 没有这样的文件或目录(LAMP 服务器)

excel - 如何区分数据与 Excel 文件中的列/标题

java - Tomcat 7.0 启动时抛出错误