Rewriting public assets to omit file extension

2019-09-04 02:18发布

问题:

For the sake of formality, I want to trim my asset URIs so that file extensions are omitted.

For example, if I request /resources/stylesheets/common, Apache will return /resources/stylesheets/common.css. At the moment, I'm using the following rewrite to get this to work:

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}.css -f
RewriteRule ^ /%{REQUEST_URI}.css [L]

This works, as it should. However, I need to also do this for other resources, like images and scripts. For simplicity, I would like to avoid repeating this approach for every type of resource.

Question: What method can I use to achieve this in one rule? I'm thinking something along these lines (though this will not work, and I can see why - perhaps Apache doesn't like guess-work):

RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}.(css|js|png) -f
RewriteRule ^ /%{REQUEST_URI}.%1 [L]