在一定条件下URL重写规则条件下、重写、在一、规则

2023-09-02 00:59:17 作者:谁浮腐朽年华

我在过程中应用URL重写规则基于一定的条件下在我的.htaccess文件,但已经得到了打,由于我接近零水平的URL重写知识使用Apache .htacces.Here是我的用例

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

我有我的字preSS应用一些插件和那些使用某些CSS和JavaScript的插件,所有我想,这些URL应该从CDN服务器提供服务,由于一些问题,我不能用一个简单的改写,这将重写URL为整个应用程序的那个东西是打破我的应用程序的功能。

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

我想,当CSS和JS都来自这个插件(插件A)或任何其他插件我想重写的URL,URL应该得到改写为

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

我不知道该怎么办这样的条件基于URL重写,任何建议将真正成为 乐于助人

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

修改 这是我目前的.htaccess文件中的根目录

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>

我创建了一个新的的.htaccess 的wp-content文件文件夹,按迈克尔建议使用以下条目

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>

但看来这是不工作的仍然是所有的CSS和JS文件被送达从本地主机,而不是网址从我的CDN server.I甚至注释掉从我的根目录的.htaccess文件

 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})

但这也并没有取得在URL中的任何变化

but this also did not made any changes in the URL

推荐答案

这可以被放置在.htaccess的根目录中。你想重写任何插件可以放在里面的() |隔开。

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]

注:以上重写所有的插件文件。如果只应的CSS和放大器; JS文件,使用

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]