0

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]
user3733648
  • 1,323
  • 1
  • 10
  • 25

2 Answers2

1

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]
TonyCool
  • 1,004
  • 1
  • 6
  • 5
0

I came up with this, which will capture the filename, and if there is an extension it will capture that too. The filename capture group then has .html appended to it. However I have not tested this with multiple / in it, but I'm sure you can adapt it as necessary.

Example on Regexr

RewriteRule ^\/(\w+)[.]?(\w)? $1.html [NC,L,NS,QSA]

Gary
  • 1,120
  • 10
  • 14