1

I have a classified ads website using a proprietary script and now I am moving it to Wordpress. I want to redirect the old URLs to Wordpress URLs.
The script has two main URL types:

  1. Category URL:
    /category/id-of-category[/page-number]/category-name.html
    eg.:
    /category/15/cars.html
    /category/15/3/cars.html (the 3 is for pagination)
    it should redirect to /?cat=15

  2. Listing URL:
    /category/id-of-category/category-name/listings/id-of-ad/ad-title.html
    eg.:
    /category/15/cars/listings/12345/selling-my-old-toyota.html
    it should redirect to /?p=12345

I want to know the exact nginx configuration that would do the permanent redirection to new URL on the same domain name.

0

1 Answer 1

0

What are you doing of the page number ?

Anyway, assuming id-of-* and page number can be represented by at least one character of range [0-9] and category-name and ad-title can be represented by at least one character of range [_-a-zA-Z0-9] :

location ~ ^/category/(\d+)/?\d*/[-\w]+\.html$ {
    return 301 $scheme://$server_name/?cat=$1;
}

location ~ ^/category/\d+/[-\w]+/listings/(\d+)/[-\w]+\.html$ {
    return 301 $scheme://$server_name/?p=$1;
}

Note that nginx regular expressions are a bit particular but very powerfull (e.g. add minus sign to alphanumeric range without the need to rewrite the entire range [-_a-zA-Z0-9] in detail as you would do with grep for instance).

Update : fixed second location catpure group.

4
  • the first location was always catching urls of the second, i think the problem is with [-\W]+
    – bml13
    Sep 26, 2014 at 5:34
  • the page number is for pagination if the category contains many listings then there will be several pages.
    – bml13
    Sep 26, 2014 at 5:37
  • I solved it follows: location ~ ^/category/\d+/[^./\"'?#]+/listings/(\d+)/[^./\"'?#]+.html$ { return 301 /?p=$1;} location ~ ^/category/(\d+)(/\d+)?/[^./\"'?#]+.html$ {return 301 /?ad-cat=$1;}
    – bml13
    Sep 26, 2014 at 5:38
  • @bml13 You are right. In fact It was matching the second regex but the capture group was wrong (category name instead of the ad id). Updated answer. Sep 26, 2014 at 11:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .