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

Htaccess redirect subdirectory extension and query string


I have url like this http://www.example.com/subdir/Subcontent?QUERY=420501 i want to redirect it to http://www.example.com/subcontent.html?QUERY=420501 so if Subcontent is dynamic in url then it will change first letter of Subcontent to small and redirect to final url http://www.example.com/subcontent.html?QUERY=420501

I have tried with this

RewriteCond %{QUERY_STRING} QUERY=([0-9]+) [NC]
RewriteCond %{REQUEST_URI} !\.html
RewriteRule ^subdir/(.*)$ /$1 [L,R=301]

But it does not seems to work i think sequence is not proper of conditions cangetting clueless on this.


Solution

  • Add this RewriteMap in Apache or vhost server config:

    RewriteMap lc int:tolower
    

    Also you have an extra condition, use this rule to redirect and lowercase your URLs:

    RewriteCond %{QUERY_STRING} (^|&)QUERY=([0-9]+) [NC]
    RewriteRule ^subdir/([\w-]+)/?$ /${lc:$1}.html [L,NC,R=301]