主目录HTML站+子目录WordPress站非www转www重定向故障排查
Let’s work through your issues step by step to get everything redirecting properly and eliminate that font CORS error:
1. Why the CORS Error Occurs
The font block happens because some requests (especially from your WordPress subdirectory) were still hitting the non-www domain, while the font files were served from the www domain. This cross-origin mismatch triggers the CORS policy block. The core problem is that your WordPress subdirectory wasn’t being included in the non-www to www redirect.
2. Update the Main Directory .htaccess
First, tweak your main .htaccess to ensure the redirect runs first and covers all subdirectories. We’ll also adjust the CORS rule to include woff2 (since that’s the font type in your error) and fix the 404 redirect to use www:
ErrorDocument 404 http://www.arabyouthsurvey.com/page404.html # Redirect non-www to www (applies to all subdirectories) RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] # Temporary CORS fix (can remove once all traffic is on www) <IfModule mod_headers.c> <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css)$"> Header set Access-Control-Allow-Origin "*" </FilesMatch> </IfModule>
3. Fix the WordPress Subdirectory .htaccess
WordPress’s internal rewrite rules use [L] flags that stop processing further rules, so your redirect needs to go before the WordPress block. Add the non-www to www redirect at the top of the /media-center/.htaccess file:
# Redirect non-www to www for media-center subdirectory RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/media-center/$1 [R=301,L] # 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
4. Critical Final Steps
- Clear Browser Cache: Old 301 redirects and resource requests get cached, so fully clear your browser’s cache before testing.
- Update WordPress Settings: In your WordPress admin, go to Settings → General and set both "WordPress Address (URL)" and "Site Address (URL)" to
http://www.arabyouthsurvey.com/media-center(using www). This ensures WordPress generates internal links with www, avoiding unnecessary redirects. - Test Everything: Verify the main site, blog posts, and font loading all work without CORS errors. Once you confirm all traffic is on www, you can safely remove the CORS header block from the main
.htaccess(since cross-origin requests will no longer occur).
内容的提问来源于stack exchange,提问作者Owais Aslam

