OpenCart Journal主题下如何将非www域名重定向至www及SSL地址?
Hey there, let's get this sorted so every way users type your domain—whether it's http://example.com, http://www.example.com, or even just typing example.com into the address bar—lands them straight on https://www.example.com. Plus, this will fix that missing styles/icons issue you're seeing with the bare domain!
1. For Apache Servers
Grab your .htaccess file (usually in your site's root folder) and drop these rules in:
RewriteEngine On # First, send all HTTP traffic to HTTPS RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] # Then, send any bare domain (no www) traffic to the www version RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Quick breakdown:
R=301is a permanent redirect—this tells search engines and browsers that this is the new permanent home for your site, which is great for SEO and means browsers will cache the redirect to speed things up next time.[NC]makes the match case-insensitive, so even if someone typesExample.comorEXAMPLE.COM, it still works.
2. For Nginx Servers
Head to your site's config file (typically in /etc/nginx/sites-available/) and update it with these server blocks:
# Catch all HTTP requests (both www and bare domain) and send to HTTPS www server { listen 80; server_name example.com www.example.com; return 301 https://www.example.com$request_uri; } # Catch bare domain HTTPS requests and redirect to www server { listen 443 ssl; server_name example.com; # You'll need an SSL cert for example.com here too—otherwise users get a security warning first ssl_certificate /path/to/your/example.com.crt; ssl_certificate_key /path/to/your/example.com.key; return 301 https://www.example.com$request_uri; } # Your main site block for https://www.example.com server { listen 443 ssl; server_name www.example.com; ssl_certificate /path/to/your/www.example.com.crt; ssl_certificate_key /path/to/your/www.example.com.key; # Add your usual site config here (root folder, index files, location rules, etc.) }
Pro tip: Grab a wildcard SSL cert (covers *.example.com) so you don't need separate certs for the bare domain and www version—it's easier to manage and avoids those annoying security warnings.
3. If You're Using Cloudflare (CDN)
This is super straightforward if you're on Cloudflare:
- Log into your Cloudflare dashboard and open your site's settings.
- Go to Rules > Page Rules.
- Create a new rule:
- URL Match:
*example.com/*(this catches every variation of your domain) - Setting: Choose "Forward URL", pick "Permanent Redirect (301)", and set the target to
https://www.example.com/$2.
- URL Match:
- Save the rule, and you're done!
Why Were Styles/Icons Missing?
That issue happened because when users hit the bare domain (example.com), your site was probably loading resources (CSS, JS, icons) using absolute paths pointing to www.example.com. Browsers can block cross-domain resources in some cases, or the paths just didn't resolve correctly on the bare domain. Once all traffic is redirected to https://www.example.com, everything loads from the same domain, so those resources will show up as expected.
内容的提问来源于stack exchange,提问作者Bilal Sheikh

