Welcome! Log In Create A New Profile

Advanced

nginx ignoring Apache rewrite directive

Posted by CrazyDogster 
nginx ignoring Apache rewrite directive
May 11, 2011 07:39AM
Hello everyone.

My problem is like this:
- I now use nginx in front of Apache server wide meaning all my websites
- all the rewrites I have in the .htaccess file work perfect, except 1 and up so far noone managed to give me a solution

This is the .htaccess rule
-------------
RewriteCond %{THE_REQUEST} ^.*/index\.(html|htm)\ HTTP/
RewriteRule ^(.*)index\.(html|htm)$ http://%{HTTP_HOST}/$1 [R=301,L]
-------------

This does the following:
- it redirects with a 301 http code from www.domain.com/index.html to www.domain.com
- it takes both index.html and index.htm

Now the thing is that this worked prior to installing nginx and works so far for index.htm.. it redirects properly.
But index.htm does not exists.

I tried the other way around and found out that if the file exists, the redirect WON'T happen.

Another weird thing that I found is the following: I also have mod_pagespeed installed from Google and when I access the URL with www.domain.com/index.html... the one that DOES NOT rewrite to /, this mod_pagespeed is... inactive.. like that URL is not from the same normal requests.

If you guys have a clue I'd happily share a virtual mug of beer with you for a good piece of info.

Thanks
Re: nginx ignoring Apache rewrite directive
May 12, 2011 01:50AM
Ok.. found some answers and I'm gonna share them with you.. maybe it helps:
----
What I found is that if the request is for a static file nginx doesn't pass this to Apache and serves this file directly.. this is why when messing with the .htaccess there was no error when requesting the URL with index.html in it.

The rewrite and redirect rules must be set in the server { } block and it is pretty simple (this I found on this forum - Sergey gave the answer in another sort of unrelated post):
-> rewrite ^(.*/)index.html http://$host$1 permanent;

This in order to work must be set either in the /etc/nginx/vhosts/domain.com ( I'm using Linux CentOS ) or in the /etc/nginx/vhosts/default - which affects all domains hosted. This is a bit more tricky and haven't got the answer for it yet but just as a heads up.

So.. to wrap it up... static files ( I'm still a newb so some stuff looks X_X ) are served by nginx directly.. this was a bit confusing as after the nginx install everything seemed to work pretty ok without any modifications to .htaccess or nginx.
----

Thanks and use it wisely :)
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 123
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready