重启EC2实例Apache服务器后WordPress网站无法访问
Let's break down why your WordPress site went down after adding those rewrite rules, and walk through the correct way to set up the www redirect without breaking anything.
First, Let's Get Your Site Back Up
Right now, the most likely culprits are either incomplete rewrite rules or a syntax error in your bitnami.conf file. Here's how to restore access quickly:
- SSH back into your EC2 instance
- Open the config file again with
sudo nano /opt/bitnami/apache2/conf/bitnami/bitnami.conf - Comment out the three rewrite lines you added (prefix each with
#) - Restart Apache using Bitnami's official script (this is critical—don't use standard apache2 commands):
sudo /opt/bitnami/ctlscript.sh restart apache
Your site should load again once this is done.
Now, Configure the WWW Redirect Correctly
Bitnami environments have specific configuration patterns, and incomplete rewrite rules (like your truncated RewriteRule) will break Apache. Here's the proper setup:
Option 1: Update the Global Bitnami Config (bitnami.conf)
Edit the bitnami.conf file again, and in the <VirtualHost _default_:80> block (below DocumentRoot "/opt/bitnami/apache2/htdocs"), add this complete rewrite rule set:
RewriteEngine On # Match hosts that don't start with www. (case-insensitive) RewriteCond %{HTTP_HOST} !^www\. [NC] # Redirect to the www version with permanent 301 redirect RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If your site uses HTTPS (which it should), you'll also need to add the same logic to the <VirtualHost _default_:443> block to redirect https://yourdomain.com to https://www.yourdomain.com:
RewriteEngine On RewriteCond %{HTTP_HOST} !^www\. [NC] RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Option 2: Use WordPress's App-Specific Config (Recommended)
Bitnami isolates app configurations for better management. For WordPress, edit its virtual host config:
- Open the file with
sudo nano /opt/bitnami/apps/wordpress/conf/httpd-vhosts.conf - Add the same rewrite rules to both the port 80 and port 443
<VirtualHost>blocks here instead of the global config. This keeps your WordPress-specific settings contained.
Before Restarting, Validate Your Config
Always check for syntax errors before restarting Apache to avoid downtime:
sudo /opt/bitnami/apache2/bin/httpd -t
If you see Syntax OK, you're good to go. If not, fix the reported errors (usually typos or missing brackets in rewrite rules).
Final Restart
Use Bitnami's script to restart Apache one more time:
sudo /opt/bitnami/ctlscript.sh restart apache
Test your site by visiting http://yourdomain.com and https://yourdomain.com—both should redirect to the www version, and your WordPress site should load normally.
Key Notes to Avoid Future Issues
- Never truncate rewrite rules: Your original
RewriteRule ^(.*)$ http://w...was incomplete, which caused Apache to fail to parse the config. - Use Bitnami's control scripts: Standard
systemctl restart apache2commands can lead to inconsistencies in Bitnami stacks. - 301 vs 302 redirects: Use
R=301for permanent redirects (best for SEO),R=302only for temporary changes.
内容的提问来源于stack exchange,提问作者tstreets

