I’m using these rewrite rules to go from dynamic urls to static ones.
RewriteEngine On
RewriteRule ^songs/album/([^/]*)\.html$ /index.html?album=$1 [L,QSA]
Old url: http://example.com/index.html?album=rockstar
New url: http://example.com/songs/album/rockstar.html
But if I try to redirect old url to new url, it doesn’t work
RedirectMatch 301 ^/index.html?album=(.*)\$ http://example.com/songs/album/$1.html
Any ideas?
The
RedirectMatchdirective in mod_alias won’t match against the query string, only the/index.htmlpart. You’d need to use aRewriteCond ${QUERY_STRING} <regexp>to match against it. But if you redirect like that you’re going to cause a loop because the URI is put through the rewrite engine until the URI comes out unchanged:/songs/album/rockstar.htmland rewrites it to/index.html?album=rockstar/index.html?album=rockstarYou need to make sure to only redirect if the actual request was for
/index.html?album=rockstar, and not when it’s been through the rewrite engine:The %{THE_REQUEST} is the actual HTTP request, and not the rewritten URI. The %1 is a backreference to a match in a previous
RewriteCondand the ? at the end of the redirect URL tells theRewriteRulenot to append the query string to the end.