Apache服务器块URL重写问题:仅首页可访问,其他链接失效
Hey there, let's tackle this 404 issue head-on. You mentioned only your homepage works while every other link throws that Apache 404 message—and since you noted past config fixes solved similar problems, we'll focus on the most likely culprits in your provided files.
First, the Most Likely Fix: .htaccess RewriteBase Misconfiguration
Looking at your .htaccess file, the biggest red flag is this line:
RewriteBase /var/www/html
RewriteBase should be a path relative to your Apache DocumentRoot (which is set to /var/www/html in both your virtual host configs). Using an absolute filesystem path here breaks the rewrite rules—Apache can't map clean URLs like /albums to the actual albums.php file correctly.
Fix Step 1: Correct the RewriteBase
Change that line to:
RewriteBase /
Second, Fix the Truncated .htaccess Content
Your .htaccess ends abruptly with:
Rewrite
This incomplete line will cause Apache to ignore or fail the rewrite rules entirely. Make sure you paste the full, complete content of your .htaccess—check for any missing rules or syntax errors at the end.
Additional Checks & Verifications
1. Ensure mod_rewrite is Enabled
Even though your .htaccess uses <IfModule mod_rewrite.c>, double-check the module is active:
sudo a2enmod rewrite
If it wasn't enabled, restart Apache afterward:
sudo systemctl restart apache2
2. Verify File Existence & Permissions
Make sure all the target PHP files (like albums.php, signup.php, inbox.php, etc.) exist in /var/www/html and are readable by the Apache user (www-data):
ls -l /var/www/html
If permissions are off, fix them with:
sudo chown -R www-data:www-data /var/www/html sudo chmod -R 755 /var/www/html
3. Check Apache Error Logs
For more detailed clues, look at Apache's error log:
tail -f /var/log/apache2/error.log
Visit a broken page like /albums while watching the log—you'll see exactly what path Apache is trying to access, which can confirm if the rewrite rules are working as expected.
Final Step: Restart Apache
After making any changes to .htaccess or Apache configs, restart the service to apply them:
sudo systemctl restart apache2
内容的提问来源于stack exchange,提问作者RushProGlobal

