.htaccesshttp-redirectmraid

Redirecting A File Anywhere In The Hierarchy


So, apparently some mobile ads drop a request for mraid.js into their page loads. That feels ridiculous to me, but there's not much I can do about that exactly. It looks like this:

If the page is /something, the request comes in as /something/mraid.js

But that also continues for cases like /something/else/mraid.js

No matter what the URL, it tacks on mraid.js.

I added a couple lines like this to the .htaccess file:

RedirectMatch 301 (.*)\mraid.js$ /mraid.js

or

RedirectMatch 301 ^\mraid.js$ /mraid.js

or

RewriteCond  %{DOCUMENT_ROOT}/(.*)/mraid.js -f
RewriteRule ^(.*)$ /mraid.js [R=301,L]

With the hopes of redirecting them to a global (and blank) mraid.js file. This is just to load a file to stop the 404 errors in analytics, from what I'm told when mraid.js is needed it's loaded locally from within the mobile ad/app.

But none of those .htaccess rules seem to catch and redirect as I expect. Any ideas on how to always match mraid.js in the URL and pass it back to root?


Solution

  • After a bit more head-scratching, I figured it out. It was a combination of all three things that solved it for me. First, it had to match the pattern with ^(.*), then [L,R=301]. L for "this is the last rule", and R for "it's a permanent 301 redirect."

    RewriteRule ^(.*)/mraid\.js$ /mraid\.js [L,R=301]