views:

956

answers:

2

Hi guys, I'm stuck with this, my skills in the web servers area are poor...

I have an Nginx acting as a proxy for an Apache2 running with mod_wsgi and mod_rewrite. What I want to do is rewrite every URL from www.example.com to example.com, i.e. stripping the www part from each URL request before serving. This is the layout of the different conf files:

=== /etc/nginx/sites-enabled/example.com ===:

http://dpaste.com/82638/

=== /etc/apache2/sites-enabled/example.com ===:

http://dpaste.com/hold/82645/

=== /home/nabuco/public_html/example.com/example/apache/example.wsgi ===:

http://dpaste.com/82643/

In my old set up I had an Apache2 running mod_python, and the only thing I had to do was putting an .htaccess file like this:

Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

That worked perfectly.

But I tried putting the same .htaccess file into /home/nabuco/public_html/nomadblue.com/nomadblue/apache/.htaccess. If I cast a request without leading www, such as http://example.com/ or http://example.com/whatever, everything goes well. However, if I try the www version of http://www.example.com/ I am redirected to:

http://example.com/example.wsgi/

Do I have to run rewriting rules from nginx instead? I tried that too, adding this to the nginx conf file:

rewrite ^/(.*) http://example.com/$1 permanent;

but now I am getting what firefox calls a "circular loop"...

So who can I get this (I guess trivial) thing up?

Thanks in advance,

Hector

A: 

The easiest is to rewrite with nginx. Put that rewrite rule in a dedicated "server" bound to www.example.com

server {
  listen 80;
  server_name www.example.com;
  rewrute ^/(.*) http://example.com/$1 permanent;
}
rzab
It doesn't work with my configuration, as my Firefox reports me when I try to load page, it is making a permanent loop. I don't know where do I have to put the rewrite, so...
nabucosound
Well, it's always better to have one server declared "default" (in listen directive) explicitly. Otherwise nginx tend to pick the first one declared in doubt, which led to these loops. Sorry for being terse. I'm glad you figured the problem out.
rzab
+1  A: 

All right I found the solution to avoid the circular loop... by creating TWO server sections in my nginx config file, one for www.example.com -- which has the rewrite rule suggested by rzab -- and the other for example.com, which contains all the rest of directives.

nabucosound