views:

1702

answers:

7

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>
A: 

Hav you made sure that mod_rewrite is loaded?

John Nilsson
A: 

Yes. I loaded it using "a2enmod rewrite", which setup a symlink in mods-enabled to point at mod_rewrite.load in mod-available. I've also tried purposely misspelling one of the rewrite rules (i.e. "Rewrite3Rule") to test that the rules are actually being picked up by apache, and apache gives me an error message as I would expect. When I fix the spelling and restart apache it restarts fine.

Myron
Reply in comments under answers, not in separate answers.
Oli
Thanks...I'm still learning the standards here.
Myron
Hmm....actually, it's limiting my comment to 300 characters, and this is 91 characters over. I guess short replies go in comments, but ones over 300 characters are fine in a seperate post?
Myron
A: 

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]

Greg
Good suggestion, but I'm still not seeing either the rewriting working or anything in the log.
Myron
+1  A: 

Hey, try this:

<IfModule mod_rewrite.c>

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

</IfModule>

Had the same problem with my server, but this worked

K Prime
A: 

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:

<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>
Vinko Vrsalovic
I've tried placing these in my /etc/apache2/apache2.conf, /etc/apache2/httpd.conf, /etc/apache2/mods-available/rewrite.conf (which is symlinked from /etc/apache2/mods-enabled). My understanding is that these rules will always apply. I'm only running one domain/virtual host here.
Myron
I tried your suggestion, and apache complains about including the port 80 part (*:80): [error] VirtualHost *:80 -- mixing * ports and non-* ports with a NameVirtualHost address is not supported, proceeding with undefined results. I took out the port number, and it restarted fine, but no redirect.
Myron
This is a case more appropriate for IRC than for Stack Overflow. There are many configurations to debug. Try #apache on irc.freenode.net if none of the suggestions here work.
Vinko Vrsalovic
You can start with http://wiki.apache.org/httpd/DebianDeb0rkification
Vinko Vrsalovic
+2  A: 

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.

Leonel Martins
Escaping the dot character will not make a difference in this case
Vinko Vrsalovic
The dot will still match a dot even if an escaped dot would only match an actual dot an unescaped dot will match anything including the dot the dot was supposed to match.
John Nilsson
You´re both right. My mistake. Answer updated.
Leonel Martins
A: 

These answers are not very helpful to me. (I'm not sure what the best way to give this feedback is, since I don't have enough "reputation" to comment or vote. :P)

I have a pretty minimal understanding of this stuff, and perhaps my problem is exacerbated by the fact that I'm not the sysadmin on my server. None-the-less, I'm not seeing an answer here to the title of this thread.

livingtech