.htaccessmod-rewriteurl-rewritingisapi-rewrite

Appending an extension automaticlly if url does not contains


I am using the following Rewrite rule

RewriteMap map txt:C:/seo/mapping.txt
RewriteCond %{QUERY_STRING} (?!admin)
RewriteCond ${map:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1} [NC,L,NS,QSA]

But there are some cases where the URL does not contains an extension

like user should be able to enter either /xyz.html or /xyzWhat I need is that if user don't enters an extension then in rewrite rule .html should be automatically appended.

Something like

RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1\.html} [NC,L,NS,QSA]

Solution

  • I would separate these two cases like this:

    RewriteMap map txt:C:/seo/mapping.txt
    RewriteCond %{QUERY_STRING} (?!admin)
    RewriteCond ${map:$1|NOT_FOUND} !NOT_FOUND
    RewriteRule ^(.*?\.(?:html|gif|jpg|png)) ${map:$1} [NC,L,NS,QSA]
    
    RewriteCond %{QUERY_STRING} (?!admin)
    RewriteCond ${map:$1.html|NOT_FOUND} !NOT_FOUND
    RewriteRule ^([^/.]+)$ ${map:$1.html} [NC,L,NS,QSA]