Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am newbie with Apache's mod_rewrite and I'm having some difficulties getting it to do what I want.

In my static directory, I have some javascript files (.js) with 2 kind of filename:

  1. xxxx.js which is the standard file name
  2. AT_xxxx.js (with prefixed filename) which has been duplicated from previous standard file name but also contains my customizations

I would like to parse requests for each standard requested javascript file (xxxx.js) to check if a customized file exists (AT_xxxx.js) including all sub-directories. Then, in this case, use the custom file instead of the standard file (perhaps by internal redirect).

I tried to figure this out for hours but something is still wrong.

Note: Also, I don't know how to find custom files in sub-directories.

DocumentRoot "/data/apps/dev0/custom/my_static"

<filesMatch "\\.(js)$">
  Options +FollowSymLinks
  RewriteEngine  on    
  RewriteCond %{DOCUMENT_ROOT}/AT_$1.js -f
  RewriteRule ^(.*[^/])/?$ %{DOCUMENT_ROOT}/AT_$1.js [QSA,L]
</filesMatch>
share|improve this question

2 Answers 2

In your RewriteCond you have a backreference ($1). But in order to use a backreference, you first have to capture something to put in it. Try something like this:

RewriteCond %{REQUEST_URI} ^/(.*).js$
RewriteCond %{DOCUMENT_ROOT}/AT_%1.js -f
RewriteRule ^(.*[^/])/?$ %{DOCUMENT_ROOT}/AT_$1.js [QSA,L]

What happens above is that the first condition checks whether the requested URI ends with .js, and if so, stores the filename (up to .js) in the RewriteCond backreference %1. Then the second one checks whether the filename (though starting with AT_) exists, and if so, rewrites to it.

share|improve this answer
    
First of all, thanks for your help. Then I have a better understanding about backreference –  Marc Sep 28 '12 at 13:45
    
To continue and finish (I hope) with my pb: the 1st condition works but backreference used for 2nd cond contains not only filename but all path (wrong regex ??). Then the 2nd check never matched because it can not find the filename with prefix. I have modified 1st but still wrong: RewriteCond %{REQUEST_FILENAME} ^/(.*).js$ RewriteCond %{DOCUMENT_ROOT}/AT_%1.js -f RewriteRule ^(.*[^/])/?$ AT_$1.js [QSA,L] –  Marc Sep 28 '12 at 13:54
    
I hate regex even if it's very powerfull ;-) –  Marc Sep 28 '12 at 13:55
    
Hereafter log output if it could help you: RewriteCond: input='/data/apps/dev0/custom/my_static/matrix/common/scripts/emxUIModal.js' pattern='^/(.*).js$' => matched RewriteCond: input='/data/apps/dev0/custom/my_static/AT_data/apps/dev0/custom/my_static/matri‌​x/common/scripts/emxUIModal.js' pattern='-f' => not-matched [perdir \.(js)$/] pass through /data/apps/dev0/custom/my_static/matrix/common/scripts/emxUIModal.js –  Marc Sep 28 '12 at 14:01

I found it:

 <filesMatch "\\.(js)$">
    # Split into two variables, $1 is everything before the filename, $2 the filename without extension
    RewriteCond %{REQUEST_FILENAME} ^(.+)/([^/]+)\.js$

    # Add AT_ prefix and check if file exists
    RewriteCond %1/AT_%2.js -f

    # Internal Redirect with new file with AT_ prefix
    RewriteRule  ^(.+)/([^/]+)\.js$ %1/AT_%2.js [NC,L]
 </filesMatch>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.