RewriteRule - redirect multi variable URL to multi variable URL

59 Views Asked by At

Our old website has a search URL structure like this:

  • example.com/Country/United States/Region/California/Area/Southern California/City/San Diego/Suburb/South Park/Type/House/Bedrooms/4/Bathrooms/3/

This is currently rewritten to point to the physical page:

  • /search/index.aspx

The parameters in the URL can be mixed up in different orders, and the URL can include one or more parameters.

We want to 301 redirect these old URLs to a new structure that is ordered in a logical way and more concise:

  • example.com/united-states/california/southern-california/san-diego/south-park/?type=house&bedrooms=4&bathrooms=3
  • example.com/united-states/california/?type=house&bedrooms=4&bathrooms=3

Is there a way with URL rewriting to interrogate the old URL, work out what parameters are existing and then write out the new URL structure?

Even if we can limit it to just the Country, Region, Area, City and Suburb, that may be good enough to at least return some results even if it's not perfect.

Also, spaces should be turned into hyphens and all text made lowercase.

I already have the RewriteRule to turn the new URL structure into a URL to point to a physical page. It's just transforming the old URL in to the new URL I need help with. I've googled endlessly and it's just beyond me!

Can anyone help? Thanks.

1

There are 1 best solutions below

0
Yaroslav On

Since you already have the old search page with rewriting rules set up for it and which is capable of parsing all parameters you need, the easiest and most appropriate solution I see here is to issue a redirect you require from this old search page's code. Just put the code that composes new URL with all parameters needed and redirects from this page - this should be a lot easier than trying to parse all these parameters in .htaccess and combine them into the new format.