bl0b
bl0b

Reputation: 936

Nginx: redirect regardless of the url structure

I recently moved a subdomain to my main domain but I also changed the url structure.

Previously I had pages like http://sub.domain.com/companies/my-company-id/year/2012/charts

When moving to the main domain, I removed all the complicated urls to juts get: http://www.domain.com/companies/my-company

I currently have the following rule: rewrite ^/companies/(.*)$ http://www.domain.com/companies/$1 permanent; but when someone go on a page like http://sub.domain.com/companies/my-company/2012/charts they get redirect to http://www,.domain.com/companies/my-company/2012/charts and get a 404.

I like to force a redirection to http://www,.domain.com/companies/my-company-id regardless of what's after the my-company-id

Upvotes: 0

Views: 118

Answers (1)

Nidhi
Nidhi

Reputation: 888

Currently the parameter $1 is having the entire URI after /companies, so you are getting redirected to the original path. You should only extract the company-id in $1. Use this:

rewrite ^/companies/(.*)/(.*)$ http://www.domain.com/companies/$1 permanent;

Here the rest of the URI after company-id will be available in the parameter $2, which is not needed in the rewrite condition.

Upvotes: 1

Related Questions