Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have domain name which contains - char. For example my-domain.com. When I use rewrite, Nginx rewrites url wrongly, browser is redirected to my.com instead of my-domain.com. What is wrong in my rewrite rule?

server {
    listen      80;
    server_name     www.my-domain.com;
    rewrite     ^/(.*) https://www.my-domain.com permanent;
}
share|improve this question
    
There doesn't appear to be anything wrong with what you have, above. I tested it with a CentOS 6.2 VM running nginx 1.0.13 and 1.2.0 using curl (curl --header "Host:www.my-domain.com" --head 127.0.0.1) and got the expected lines: HTTP/1.1 301 Moved Permanently and Location: https://www.my-domain.com. Your problem lies elsewhere. (the dash does need to be escaped (i.e. \-) when used in a regular expression - however, neither your server_name nor your rewrite destination are regexes). (Also, you can simplify your rewrite to use ^ instead of ^/(.*) if you aren't using the capture.) –  cyberx86 Apr 29 '12 at 18:57
    
Its very odd, firefox is redirecting to wrong url, but not other browsers. Is firefox caching permanent redirects? –  newbie Apr 29 '12 at 19:29
    
Start with Ctrl+F5, then if that doesn't work, clear your Firefox cache and restart Firefox, use private browsing mode, and check the Location header with Firebug. Keep in mind, it is a 'permanent' redirect, so it should be cached by the browser/ intermediate proxies. You may also want to change the destination of your rewrite (e.g. www1 or /test1, etc) so that you can verify that you aren't loading a cached redirect. –  cyberx86 Apr 29 '12 at 19:37
    
It was firefox's cache, I had tried some other settings before and apparently those had something wrong settings and those redirects were cached. –  newbie Apr 29 '12 at 20:00
add comment

1 Answer

up vote 2 down vote accepted

There doesn't appear to be anything wrong with the server block you have used.

I tested the exact block you provided, with a CentOS 6.2 Virtual Machine running nginx 1.0.13 and nginx 1.2.0. I used curl to provide the matching Host header:

curl --header "Host:www.my-domain.com" --head 127.0.0.1

The response showed no error, successfully pointing to the new destination (note the 'Location' header):

HTTP/1.1 301 Moved Permanently
Server: nginx/1.2.0
Date: Sun, 29 Apr 2012 18:56:45 GMT
Content-Type: text/html
Content-Length: 184
Connection: keep-alive
Location: https://www.my-domain.com

It is worth noting that the dash is a special character in regular expressions, and as a result, does need to be escaped (i.e. \-) when used in a regex. In your server block, however, neither your server_name nor your rewrite destination are regexes (although, it is possible for a server_name to be a regex).

As an aside, if you aren't using the capture, you can simplify your rewrite to:

rewrite     ^ https://www.my-domain.com permanent;

It is quite probable that your redirect has been cached by your browser (it is a 'permanent' redirect, so it could be cached by the browser/ intermediate proxies). Some suggestions for dealing with that may include:

  • Force a refresh with: Ctrl+F5
  • Clear your browser cache and restart the browser
  • Use private browsing mode
  • Check the Location header with your developer tools (e.g. Firebug)
  • Flush your DNS cache

When testing such configurations, you may want to make a visible change with each modification (e.g. www1 or /test1, etc) so that you can verify that you aren't loading cached content.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.