Rewrite rule regexp

I would like to ask you guys because I have a problem with one my rewrite rules and I can’t figure it out how to write the good one.
I have this rule:

RewriteRule ^wp-content/uploads/(.*[^(.js|.swf)])$ authenticate.php?file=$1

What I would like to do is redirect the user to the authenticate.php every time when someone tries to open something in the wp-content uploads dir and I would like to send the filename to the php

Read More

For example:

http://domain.tld/wp-content/uploads/2015/11/something.pdf

redirect to authenticate.php?file=something.pdf

But unfortunately my regexp is broken. Could someone help me?

Really thanks for it!

Related posts

2 comments

  1. Try with that in your .htaccess:

    RewriteCond %{REQUEST_URI} !(?:js|swf)$ [NC]
    RewriteRule ^wp-content/uploads/(.+)$ authenticate.php?file=$1 [NC,L]
    

    For http://domain.tld/wp-content/uploads/2015/11/something.pdf the result: http://domain.tld/authenticate.php?file=2015/11/something.pdf

  2. Try using this regex with negative lookaheads:

    ^.*?wp-content/uploads/.*?.(?!js$|swf$)[^.]+$
    

    The following URL will match the regex:

    http://cpsma.ie/wp-content/uploads/2015/09/CPSMA-Newsletter-No-35-Sept-2015-2.pdf
    

    However, a similar URL which ends in .js or .swf will not match. Hence, the following two URLs do not match the regex:

    http://domain.tld/wp-content/uploads/2015/10/javascript.js
    http://domain.tld/wp-content/uploads/2015/02/shockwavefile.swf
    

    You can test this regex out here:

    Regex101

Comments are closed.