WordPress/Apache重写/重定向规则和正则

发布于 2025-01-24 16:59:57 字数 571 浏览 2 评论 0原文

在特定文件扩展程序的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

留一抹残留的笑 2025-01-31 16:59:57

在您的样品中,尝试;请尝试遵循HTACCESS规则文件。这些规则按照显示的域名编写,即样品/测试,因此您需要根据系统在系统中使用它们的实际值来更改值。我们还需要确保(test.com和test1.com)在您的实际Apache服务器中共享相同的目录结构。

另外,请确保在测试URL之前清除浏览器缓存。

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ https://test1.com/$1 [R=301,L]

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.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ https://test1.com/$1 [R=301,L]
过度放纵 2025-01-31 16:59:57

表格的“重定向” URL /folder/subfolder/<年>/</>/gt;/< file>。png | png | jpg | jpg | gif | gif | svg | svg | svg | svg | svg> were code>/code>/////文件夹/子文件夹/是静态的,其他元素是可变的,并且在文件系统上不存在您需要在WordPress代码块之前进行以下操作。 IE。 #开始WordPress部分。

# Redirect certain non-existent image files to another server
RewriteRule ^folder/subfolder/\d{4}/\d\d/[\w-]\.(png|jpg|gif|svg)$ https://test1.com/$0 [R=302,L]

# BEGIN WordPress
:

<年是一个4位数字,< monter>是2位数字。该文件名可以由字符组成0-9azaz_(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.

# Redirect certain non-existent image files to another server
RewriteRule ^folder/subfolder/\d{4}/\d\d/[\w-]\.(png|jpg|gif|svg)$ https://test1.com/$0 [R=302,L]

# BEGIN WordPress
:

The <year> is a 4-digit number and <month> is a 2-digit number. The filename can consist of the characters 0-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 with P (proxy).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文