URL Rewrite rule based on certain conditions

I am in process to apply URL rewriting rule based on certain condition in my .htaccess file but have got struck due to my near to zero level knowledge of URL rewriting using Apache .htacces.Here is my use case

i have few plugins in my WordPress application and those plugins making use to certain CSS and JavaScript,all i want that those URLs should be served from the CDN server, due to some issues i can not use a simple rewrite which will rewrite the URL for entire application as that thing is breaking my application functionality.

Read More
http://www.mydomain.com/wp-content/plugins/pluginA/abc.css
http://www.mydomain.com/wp-content/plugins/pluginA/abc.js

i want that when the CSS and JS are from this plugin (Plugin A ) or any other plugin whose URL i want to rewrite, The URL should get rewritten as

http://www.cdn.mydomain.com/wp-content/plugins/pluginA/abc.css
http://www.cdn.mydomain.com/wp-content/plugins/pluginA/abc.js

i am not sure how to do such conditional based URL rewriting,Any suggestion will really be
helpful

Edit
This is my current .htaccess file in the root

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

i have created a new .htaccess file under wp-content folder as per Michael suggestion with following entry

RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase  /wordpress/
RewriteCond %{REQUEST_FILENAME} ^.+.(css|js)$
RewriteRule wp-content/plugins/(nivo-slider-light|Usernoise|lightbox-plus|wp-content-slideshow|content-slide)/(.+)$ http:/cdn.cloudfront.net/wp-content/plugins/$1/$2 [L,R=302,QSA]
</IfModule>

but seems this is not working as still for the above plugins all the css and js file are being served with the URL from localhost and not from my CDN server.I even commented out these lines from my root’s .htaccess file

 RewriteCond %{QUERY_STRING} (<|%3C).*script.*(>|%3E) [NC,OR]
 RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
 RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})

but this also did not made any changes in the URL

Related posts

Leave a Reply

1 comment

  1. This could be placed in .htaccess inside the root directory. Any plugins you want rewritten can be placed inside the () separated by |.

    RewriteEngine On
    # Rewrite pluginA plugin X, and pluginZ to the CDN. Add add more with |
    RewriteRule wp-content/plugins/(pluginA|pluginX|pluginZ)/(.+)$ http:/www.cdn.mydomain.com/wp-content/plugins/$1/$2 [L,R=301,QSA]
    

    Note: the above rewrites all the plugin files. If it should only be the css & js files, use

    RewriteCond %{REQUEST_FILENAME} .(css|js)$
    RewriteRule wp-content/plugins/(pluginA|pluginX|pluginZ)/(.+)$ http:/www.cdn.mydomain.com/wp-content/plugins/$1/$2 [L,R=301,QSA]