Saravana Prakash Saravana Prakash - 2 months ago 14
Apache Configuration Question

Need help writing a RewriteRule to strip selectors in URL

I need to write a simple rewriterule in apache for 2 conditions


  1. Remove date selector from the js name

  2. change the location /js/ into /jsmin/



Input URL:
/etc/designs/myapp/clientlibs/basefooter/js/foresee/foresee-trigger.20160819.js

Output URL:
/etc/designs/myapp/clientlibs/basefooter/jsmin/foresee/foresee-trigger.js

I tried below Rule:

RewriteRule ^/etc/designs/myapp/clientlibs/basefooter/js/(.*).(0-9).js /etc/designs/myapp/clientlibs/basefooter/js/$1.js
RewriteRule ^/etc/designs/myapp/clientlibs/basefooter/js/(.*).js /etc/designs/myapp/clientlibs/basefooter/jsmin/$1.js


Note:


  • All js are under /etc/designs/myapp/clientlibs/basefooter/js and get
    minified by maven during build into /jsmin

  • There are more sub directories under js like /js/foresee/foresee.js



It works for the second rule, but first rule fails. Can you please help to write the rule?

Answer

Your regex needs to be modified

(.*)\.([0-9].+)\.js

Explanation

  1. First group of brackets would match the folder and file name /etc/designs/myapp/clientlibs/basefooter/js/foresee/foresee-trigger.20160819.js
  2. Second group of brackets would match the date /etc/designs/myapp/clientlibs/basefooter/js/foresee/foresee-trigger.20160819.js