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;
	if ( $uri !~ ^/*(.css|.ico|.jpg|.js|.png) ) {
		rewrite ^(.*)$1 permanent;

Open in new window

LVL 58
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.