htaccess 更改后 wordpress 站点丢失图像 [英] wordpress site missing images after htaccess change

查看:48
本文介绍了htaccess 更改后 wordpress 站点丢失图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在根目录中安装了一个 wordpress,在根目录下的子文件夹中安装了另一个.

I have a wordpress installation in the root and another one in a subfolder within the root.

通常会发生的情况是网址如下所示:

What would normally happen is that the url would look like:

https://example.com/quotes/us/some-url

但我想从网址中删除引号",所以结果就像:

but I wanted to remove 'quotes' from the url so it just ended up like:

https://example.com/us/some-url

多亏了另一个堆栈溢出用户,我能够使用下面的 htaccess 代码让它工作,但我没有意识到图像现在没有显示,而且我收到了所有这些图像的 404 错误.这是根 .htaccess 文件

Thanks to another stack overflow user, I was able to get that to work with the below htaccess code but I didn't realise that the images are now not showing and I get a 404 error for all of them. This is the root .htaccess file

RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]

# BEGIN rlrssslReallySimpleSSL rsssl_version[3.3.5]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END rlrssslReallySimpleSSL
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

和子文件夹'quotes' .htaccess 看起来像这样

and the subfolder 'quotes' .htaccess looks like this

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /quotes/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /quotes/index.php [L]
</IfModule>
# END WordPress

推荐答案

此路径出现 404 错误https://example.com/wp-content/themes/theme_quotes/style.css?ver=1.0.0.因此,它在根安装中查找该主题不存在的地方,因为它只存在于子目录安装中

I get a 404 error to this path https://example.com/wp-content/themes/theme_quotes/style.css?ver=1.0.0. So, it is looking in the root installation where that theme doesn't exist as it only exists in the subdirectory installation

目前,当 URL 路径以 2 个字母的语言代码开头时,我们只重写对 /quotes 子目录的请求,因为这是唯一可以区分两者之间 URL 的地方WordPress 安装.但是,这意味着没有语言代码前缀(并且不直接引用 /quotes 子目录)的静态资源的 URL(如上所述)不会被重写,因此会以 404 失败.

Currently, we only rewrite requests to the /quotes subdirectory when the URL-path starts with a 2-letter language code, since that is the only thing that appears to differentiate the URLs between the two WordPress installs. However, that means that URLs to your static resources (as above) that do not have the language code prefix (and do not reference the /quotes subdirectory directly) are not being rewritten and so fail with a 404.

这也许可以在 WordPress 中解决,方法是在静态资源的 URL 中包含 /quotes.但这确实为查看您的 HTML 源代码的任何人公开了 /quotes 子目录.我们还需要修改 /quotes/.htaccess 文件中的重定向指令,以防止这些请求被重定向回 root.实际上,您的图像似乎已经发生了这种情况,这些图像已经包含完整(正确")的 URL 路径.

This could perhaps be fixed in WordPress, by including /quotes in the URL to your static resources. But that does expose the /quotes subdirectory for anybody looking at your HTML source. We would also need to modify the redirect directive in the /quotes/.htaccess file to prevent these requests being redirected back to root. Actually, it looks like this is happening with your images already which already include the full ("correct") URL-path.

我们可以做什么... 在根 .htaccess 文件中,将任何对静态资源(图像、CSS 或 JS 文件)的请求重写到 /quotes 子目录(如果根目录中不存在).例如:

What we could do... in the root .htaccess file, rewrite any request for a static resource (image, CSS or JS file) to the /quotes subdirectory if it doesn't exist in the root. For example:

# Rewrite any URLs that contain a language code prefix to the subdirectory
RewriteRule ^[a-z]{2}/ quotes%{REQUEST_URI} [L]

# Rewrite any request for a static resource that does not exist (in the root)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(css|js|png|jpg|webp|gif)$ quotes%{REQUEST_URI} [L]

# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)

这确实意味着如果您在两个安装中都有两个同名的静态资源(相同的基本 URL 路径),那么根安装中的一个将获胜".

This does mean that should you have two static resources with the same name (same base URL-path) in both installations then the one in the root installation will "win".

请注意,这是一个盲"重写...如果在任一安装中都不存在特定的静态资源,那么您将始终在 /quotes 安装中获得 404.但是没有办法真正解决这个问题,因为 URL 路径结构中存在歧义.

Note that this is a "blind" rewrite... if a particular static resource does not exist in either installation then you will always get the 404 in the /quotes installation. But there's no way to really resolve that since there is an element of ambiguity in the URL-path structure.

AND,/quotes/.htaccess 文件中,防止任何对静态资源的直接请求被重定向回根.例如:

AND, in the /quotes/.htaccess file, prevent any direct requests for static resources being redirected back to the root. For example:

# Redirect any direct requests for "/quotes/<anything>" back to root
# Except for static resources
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|webp|gif)$
RewriteRule (.*) /$1 [R=301,L]

# BEGIN WordPress
# : (Remainder of existing .htaccess file goes here)

我假设您所有的文件扩展名(静态资源)都是小写的.

I'm assuming all your file extensions (to static resources) are lowercase.

您需要清除浏览器缓存,因为重定向回 root 的图像很可能已被浏览器缓存(因为这是一个 301 - 永久 - 重定向).

You will need to clear your browser cache, since the image redirect back to root will likely have been cached by the browser (since this is a 301 - permanent - redirect).

这篇关于htaccess 更改后 wordpress 站点丢失图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆