WordPress/Apache重写/重定向规则和正则
在特定文件扩展程序的404错误时,我需要对Apache重写/重定向规则进行外部Web服务:.jpg,.png等。此处使用WordPress。
因此,如果404发生在:
https://test.com/folder/subfolder/year/year/month/filename.jpg
我想将其重定向到:
https://ttps://test1.com.com /folder/subfolder/year/month/filename.jpg
(外部WebService,而不是同一phisical Server)
我在htaccess中尝试了这样的配置,没有按预期工作:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) test1.com/folder/subfolder//$year$\/$month$\/([^\s]+(\.(?i)(png | jpg | gif | svg))$)/
您是否有任何想法做正确的方法吗?
任何建议都赞赏。
I need to do some apache rewrite/redirect rules to external webservice in case of 404 error for specific file extensions: .jpg, .png, etc. Wordpress is used here.
So, if 404 occurs at:
https://test.com/folder/subfolder/year/month/filename.jpg
I want to redirect it to:
https://test1.com/folder/subfolder/year/month/filename.jpg
(external webservice, not the same phisical server)
I've tried such a configuration in htaccess, didn't work as expected:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) test1.com/folder/subfolder//$year$\/$month$\/([^\s]+(\.(?i)(png | jpg | gif | svg))$)/
Do you have any ideas how to do it right way?
Any suggestions appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在您的样品中,尝试;请尝试遵循HTACCESS规则文件。这些规则按照显示的域名编写,即样品/测试,因此您需要根据系统在系统中使用它们的实际值来更改值。我们还需要确保(test.com和test1.com)在您的实际Apache服务器中共享相同的目录结构。
另外,请确保在测试URL之前清除浏览器缓存。
With your shown samples, attempts; please try following htaccess rules file. These rules are written as per shown domain names which are samples/tests, so you need to change values as per your actual values when you use them in your system. We also need to make sure that both (test.com and test1.com) are sharing same directory structure in your actual apache server.
Also make sure to clear your browser cache before testing your URLs.
表格的“重定向” URL
/folder/subfolder/<年>/</>/gt;/< file>。png | png | jpg | jpg | gif | gif | svg | svg | svg | svg | svg>
were code>/code>//
/
/
/文件夹/子文件夹/
是静态的,其他元素是可变的,并且在文件系统上不存在您需要在WordPress代码块之前进行以下操作。 IE。#开始WordPress
部分。<年
是一个4位数字,< monter>
是2位数字。该文件名可以由字符组成0-9
,az
,az
,_
(UNDERSESCORE)和-
(连字符)。这大概应该是302(临时)重定向,而不是301(永久性),否则,如果应该在源域上可用资源,那么那些之前访问过URL的用户将无法访问它(他们将被重定向来自缓存)。
为了避免外部重定向,它可能比“代理”请求“代理”到其他域。 (最终用户是看不见的。)尽管这可能涉及其他配置服务器端,因为您需要将源服务器配置为“ 反向代理”。然后,您可以用
p
(代理
)替换上述规则中的r = 302
标志。To "redirect" URLs of the form
/folder/subfolder/<year>/<month>/<file>.<png|jpg|gif|svg>
where/folder/subfolder/
is static and the other elements are variable and which do not exist on the filesystem you would need to do something like the following before the WordPress code block. ie. before the# BEGIN WordPress
section.The
<year>
is a 4-digit number and<month>
is a 2-digit number. The filename can consist of the characters0-9
,a-z
,A-Z
,_
(underscore) and-
(hyphen).This should presumably be a 302 (temporary) redirect, not a 301 (permanent), otherwise if the resource should become available at the source domain then it won't be accessible to those users who have visited the URL before (they will be redirected from cache).
To avoid the external redirect it may be preferable to "proxy" the request to the other domain. (This is invisible to the end user.) Although this potentially involves additional configuration server-side, as you would need to configure the source server as a "reverse proxy". You can then replace the
R=302
flag in the above rule withP
(proxy
).