URL Rewrite rule based on certain conditions

混江龙づ霸主 提交于 2019-12-11 11:32:37

问题


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.

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


回答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]


来源:https://stackoverflow.com/questions/10164476/url-rewrite-rule-based-on-certain-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!