• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 307
  • Last Modified:

Nginx rewrite unless...

I am trying to do a rewrite on non www to the www version unless it is of types .css/.js etc
I thought the below would work but it is still redirecting - what am I missing

server {
	listen  80;
	server_name  mysite.com;
	if ( $uri !~ ^/*(.css|.ico|.jpg|.js|.png) ) {
		rewrite ^(.*) http://www.mysite.com$1 permanent;

Open in new window

  • 2
  • 2
1 Solution
You're missing a period, try
if ( $uri !~ ^/.*(.css|.ico|.jpg|.js|.png) ) {

Open in new window

GaryAuthor Commented:
Ok I may be a tad tipsy, what does that do (it works but I'm not seeing how)
You had:  ^/*(.css|.ico|.jpg|.js|.png)  indicating you were looking for URL's that only consisted of an extension e.g.


The change was to add a "." period, which combined with the existing "*", results in a regexp that will match any string before the extention e.g.

GaryAuthor Commented:
Ahhh jaysus understood - this is why I hate regex

Many thanks
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now