如何排除为什么不是由阿帕奇采用我重写规则? [英] How do I troubleshoot why my rewrite rules aren't being applied by apache?

查看:102
本文介绍了如何排除为什么不是由阿帕奇采用我重写规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与Apache httpd的作为前端运行一个Tomcat 6 Web应用程序。我使用mod_proxy和mod_proxy_ajp的转发请求到Tomcat。我的服务器运行的是Ubuntu Linux系统。现在,我试图使用mod_rewrite删除前导WWW,让自己的规范网站网址 http://domain.com 而不是 http://www.domain.com

我读过关于使用mod_rewrite一些教程,但我不能得到任何重写工作。我试图把在.htaccess文件重写规则(修改我的/ etc /阿帕奇/站点可用/默认文件来设置所有的AllowOverride之后)。我试过把重写规则在apache2.conf,httpd.conf文件,并rewrite.conf。我已经试过所有这些与重写日志功能。日志文件被创建,但Apache的写没有什么不可以。我想也许mod_proxy的在某种程度上preventing被用于重写规则,所以我尝试禁用该以及...我仍然没有得到改写,并没有到日志中。

在这一点上,我完全不知道下一个尝试的东西。我该如何去了解为什么Apache不使用我重写规则解决问题。

有关参考,在这里是我的改写指令:

 < IfModule mod_rewrite.c>    RewriteEngine叙述上
    的RewriteCond%{HTTP_HOST} ^ $ www.domain.com [NC]
    重写规则^(。*)$ http://domain.com/$1 [R = 301,L]
    RewriteLog/var/log/apache2/rewrite.log
    RewriteLogLevel 3< / IfModule>

修改:下面的反应是有助于我的具体情况,但可能不会作为有助于社区在超大因为你是如何在总体解决Apache指令答案。例如,有一种方法启用日志记录的地步,它会告诉我哪些指令以什么顺序的请求到达被应用?

编辑2 :我已经得到的东西现在的工作。我的虚拟主机都不太树立正确的,我也不太有重写正则表达式正确。下面是最终改写指令,我得工作:

 < IfModule mod_rewrite.c>
        RewriteEngine叙述在
        的RewriteCond%{HTTP_HOST} ^万维网\\。域\\ .COM [NC]
        (。*)重写规则^ $ HTTP://domain.com$1 [L,R = 301]
< / IfModule>


解决方案

试图回答你的问题:要调试Apache操作可以调整的 LogLevel的以一个较低的水平(也许调试)。但是,即使你把调试如果禁用日志有问题的模块,你不从它那里得到任何消息。例如,默认的 [RequestLogLevel] [2] 0,例如​​,模块不写任何消息。我看你把它设置好的3但像<一个href=\"http://stackoverflow.com/questions/215316/how-do-i-troubleshoot-why-my-rewrite-rules-arent-being-applied-by-apache#215345\">RoBorg说其更改为 9 ,也许是太低,你的情况。

试图SOVE您的问题:尝试改变你使用逆形式重写主机的方式 - 看,如果主机名是你想要的,如果不是,将其更改为你想要的主机名。像 URL重写指南中的规定 - Apache HTTP服务器在Apache网站


  

主机名典型


  
  

说明
      这一规则的目的是强制使用一个特定的主机名,在
  preference到其他主机名其中
  可用于达到相同的位点。
  例如,如果你想强制
  使用www.example.com代替
  example.com,你可以使用一个变种
  下面的食谱。解决方法:

 #要强制使用
RewriteEngine叙述在
的RewriteCond%{HTTP_HOST} ^!WWW \\ .EXAMPLE \\ .COM [NC]
的RewriteCond%{HTTP_HOST}!^ $
重写规则^ /(。*)http://www.example.com/$1 [L,R]


在他们的榜样,他们从 example.com 更改为 www.example.com 但你有这个想法。刚刚调整它你的情况。

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.com rather than 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.

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?

For reference, here are my rewrite directives:

<IfModule mod_rewrite.c>

    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    RewriteRule ^(.*)$ http://domain.com/$1 [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?

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:

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

解决方案

Trying to answer your question: To debug Apache operation you can adjust the LogLevel to a lower level (maybe debug). But even if you put debug if 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 RoBorg said change it to 9 that maybe is too low for your case.

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 Server at the Apache Site:

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]

In their example, they change from example.com to www.example.com but you got the idea. Just adjust it for your case.

这篇关于如何排除为什么不是由阿帕奇采用我重写规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆