regex.htaccess

Regex append .html to subdirectory files


On Apache, I am looking to append .html to all URLs that appear within the subdirectory /article.

In the htaccess file, I have tried

RewriteRule ^(article\/[a-z].*)$ $1.html [NC,L]

but this does not work. Thanks in advance for any assistance.


Solution

  • Firstly, let's look at the regex you have:

    ^(article/[a-z].*)$

    This matches exactly the string "article/", followed by at least one letter (case insensitive due to the NC flag), followed by zero or more of anything. It's quite broad, but should match the examples you gave.

    One way to test that it's matching is to add the R=temp flag to the rule, which tells Apache to redirect the browser to the new URL (I recommend using "=temp" to stop the browser caching the redirect and making later testing harder). You can then observe (e.g. in your browser's F12 debug console) the original request and the redirected one.

    RewriteRule ^(article/[a-z].*)$ $1.html [NC,L,R=temp]

    However, as CBroe points out, your rule will match again on the target URL, so you need to prevent that. A simple way would be to use the END flag instead of L:

    Using the [END] flag terminates not only the current round of rewrite processing (like [L]) but also prevents any subsequent rewrite processing from occurring in per-directory (htaccess) context.

    So:

    RewriteRule ^(article/[a-z].*)$ $1.html [NC,END]

    Alternatively, you can make your pattern stricter, such as changing the . ("anything") to [^.] ("anything other than a dot"):

    ^(article/[a-z][^.]*)$

    To be even more specific, you can add a RewriteCond with an extra pattern to not apply the rule to, such as "anything ending .html".