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

What would the correct syntax be for this 301 with a query string in .htaccess?


I have several url's built like this:

/news/report_detail.asp?id=100&edition=14

I'd like to add a rewrite in .htaccess, so they look like this:

report-detail-asp-id-100-edition-14

Any help would be greatly appreciated.

Chris


Solution

  • This should work.

    RewriteCond %{QUERY_STRING} id=([0-9]+)&edition=([0-9]+)
    RewriteRule news/report_detail.asp news/report-detail-asp-id-%1-edition-%2? [R=301,L]
    

    Note that I am making an assumption that this is in your root folder's .htaccess file and you have a RewriteBase / directive already, and that you left off news/ from the beginning of the redirection URL above. Otherwise you may need to make some tweaks to this code. But this will take the user from /news/report_detail.asp?id=100&edition=14 to /news/report-detail-asp-id-100-edition-14