首页 > 解决方案 > 主目录 HTML 和子目录 WordPress 网站上的非 www 到 www 重定向不起作用

问题描述

我有一个在主域上运行的 HTML 网站,我通过在主目录的.htaccesshttp://arabyouthsurvey.com上添加一些代码,在其上添加了非 www 到 www 重定向。现在我在子目录中有一个 WordPress 博客,例如.http://arabyouthsurvey.com/media-center

首先,我需要重定向所有非 www 的 URL,包括我的博客应该用 www 重定向。

我面临的问题是:

如果我在主目录的 .htaccess 上添加重定向。它仅重定向 HTML 网站的 URL,但是,它阻止了我博客上的网络字体。

在主 .htaccess 文件上添加重定向后,我在博客中遇到了这个错误

Access to Font at 'http://www.arabyouthsurvey.com/assets/fonts/karbon-regular-webfont.woff2' from origin 'http://arabyouthsurvey.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://arabyouthsurvey.com' is therefore not allowed access.

如果我也在我的博客 .htaccess 文件中添加重定向。它不工作。

这是我在主目录 .htaccess 文件中添加的代码

ErrorDocument 404 http://arabyouthsurvey.com/page404.html

# redirect non www to www
RewriteEngine On 
RewriteCond %{HTTP_HOST} !^www\. 
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|font.css|css)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

这是我的 WordPress .htaccess

# BEGIN WordPress
<IfModule mod_rewrite.c>
 RewriteEngine On

 RewriteBase /media-center/

 RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
 RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

 RewriteCond %{REQUEST_FILENAME} -f [OR]
 RewriteCond %{REQUEST_FILENAME} -d
 RewriteRule ^ - [L]
 RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
 RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
 RewriteRule . index.php [L]
</IfModule>

# END WordPress

标签: wordpressapache.htaccessredirect

解决方案


推荐阅读