Search code examples
.htaccesshttp-redirecthttp-status-code-301

htaccess variable 301 redirect from URL (but with odd structure)


So I'm doing a site design and the URLS are changing. However, the old site has a very odd URL structure that I'm finding it hard to redirect.

Basically, just needing to grab the variable from the URL, and then pass it to another page that then does the redirect. HOWEVER, here is my issue. The old urls are NOT actually query strings.

/rental/house.html&ID=405313

Notice how it is missing the normal ? instead of the & ? Odd, right?

So currently, I have

RewriteCond %{QUERY_STRING} (^|&)ID=([^&]+)($|&)
RewriteRule ^rental/house\.html$ /newredirect.cfm?id=%1 [L,R=301]

However, this only works IF I have the ? in the url, such as....

/rental/house.html?ID=405313

Furthermore, it's not passing the variable so I obviously have an issue there as well.

Any help is greatly appreciated! Thank you!


Solution

  • Strings behind the ? are query string. In this case your request URI is /rental/house.html&ID=405313 and you can check the request URI. %1 is the backreference to ([0-9]+).

    RewriteCond %{REQUEST_URI} &ID=([0-9]+)
    RewriteRule ^/?rental/house.html /newredirect.cfm?id=%1 [L,R=301]