我如何解决为什么我的重写规则没有被 apache 应用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/215316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:53:50  来源:igfitidea点击:

How do I troubleshoot why my rewrite rules aren't being applied by apache?

apachetomcatmod-rewrite

提问by Myron Marston

I've got a tomcat 6 web app running with apache httpd as the front end. I'm using mod_proxy and mod_proxy_ajp to forward the requests to tomcat. My server is running ubuntu. Now I'm trying to use mod_rewrite to remove the leading www, so that my canonical website url is http://domain.comrather than http://www.domain.com

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

I've read a number of tutorials on using mod_rewrite, but I can't get any rewriting to work. I've tried putting the rewrite rule in an .htaccess file (after modifying my /etc/apache/sites-available/default file to set AllowOverride all). I've tried putting the rewrite rule in apache2.conf, httpd.conf, and rewrite.conf. I've tried all of these with rewrite logging turned on. The log file gets created, but apache has written nothing to it. I thought maybe mod_proxy was somehow preventing the rewrite rules from being used, so I tried disabling that as well...and I still get no rewrite, and nothing to the log.

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

At this point I have absolutely no idea what to try next. How do I go about troubleshooting why apache isn't using my rewrite rules?

在这一点上,我完全不知道接下来要尝试什么。我如何解决 apache 不使用我的重写规则的原因?

For reference, here are my rewrite directives:

作为参考,这里是我的重写指令:

<IfModule mod_rewrite.c>

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

</IfModule>

Edit: the responses below are helpful to my particular case, but probably not as helpful to the community-at-large as answers about how you troubleshoot apache directives in general. For example, is there a way to enable logging to the point where it would tell me which directives are being applied in which order as the request comes in?

编辑:下面的回答对我的特殊情况有帮助,但对整个社区的帮助可能不如有关您如何对 apache 指令进行故障排除的回答一般。例如,有没有一种方法可以让日志记录到它会告诉我在请求进入时以哪个顺序应用哪些指令?

Edit 2: I've gotten things to work now. My virtual hosts weren't quite set up right, and I also didn't quite have the rewrite regex right. Here is the final rewrite directives I got to work:

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

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

采纳答案by Leonel Martins

Trying to answer your question: To debug Apache operation you can adjust the LogLevelto a lower level (maybe debug). But even if you put debugif you disable the Log for the module in question you dont get any messages from it. For instance, the default [RequestLogLevel][2]is 0, e.g., the module dont write any messages. I see you setted it to 3 but like RoBorgsaid change it to 9that maybe is too low for your case.

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

Trying to sove your problem: Try to change the way you rewrite the hostname using the inverse form - look if the hostname is what you want and, if not, change it to the hostname you want. Like stated in the URL Rewriting Guide - Apache HTTP Serverat the Apache Site:

尝试解决您的问题:尝试更改使用逆形式重写主机名的方式 - 查看主机名是否是您想要的,如果不是,请将其更改为您想要的主机名。就像URL 重写指南 -Apache 站点上的Apache HTTP 服务器中所述:

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/ [L,R]

规范主机名

描述:此规则的目标是强制使用特定的主机名,优先于可能用于访问同一站点的其他主机名。例如,如果您希望强制使用 www.example.com 而不是 example.com,您可以使用以下配方的变体。解决方案:

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

In their example, they change from example.comto www.example.combut you got the idea. Just adjust it for your case.

在他们的例子中,他们从example.com变为www.example.com但你明白了。只需根据您的情况进行调整。

回答by K Prime

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    RewriteRule ^(.*)$ "http\:\/\/domain\.com\/" [R=301,L]

</IfModule>

Had the same problem with my server, but this worked

我的服务器有同样的问题,但这有效

回答by Vinko Vrsalovic

In which context are these rules placed? You might be placing them under a different virtual host, for example. Try to put them outside any container if you have only one domain to test.

这些规则放在什么上下文中?例如,您可能将它们放置在不同的虚拟主机下。如果您只有一个要测试的域,请尝试将它们放在任何容器之外。

In any case, there's an alternative to achieve 'no-www', consists in using two virtual hosts, one for www and another for 'no-www'. www one redirects to the other one:

在任何情况下,都有一种实现“no-www”的替代方法,即使用两个虚拟主机,一个用于 www,另一个用于“no-www”。www 一个重定向到另一个:

<VirtualHost *:80>
    ServerName www.domain.com
    Redirect permanent / http://domain.com
</VirtualHost>


<VirtualHost *:80>
    ServerName domain.com
    #The rest of the configuration (proxying, etc.)
</VirtualHost>

回答by Greg

Try increasing the logging level up to 9 (the maximum).
Be sure apache has the proper rights to the log file (although if it created it, it seems likely it could write to it too).
Try a different rewrite rule, with no condition, for example RewriteRule .* www.google.com [RL]

尝试将日志记录级别提高到 9(最大值)。
确保 apache 对日志文件具有适当的权限(尽管如果它创建了它,它似乎也可以写入它)。
尝试不同的重写规则,例如,没有条件RewriteRule .* www.google.com [RL]