AWS EC2实例仅首页可路由,后续页面报URL NOT FOUND问题求助
Hey there, let's figure out why only your homepage is working after upgrading PHP from 5.6 to 7.0 on your AWS EC2 instance. That "URL NOT FOUND" error on other pages is almost always tied to Apache configuration changes that come with PHP version updates—here's how to fix it step by step:
Pretty URLs (the kind most apps and CMS use) rely on Apache's mod_rewrite module, and it's possible the PHP upgrade disabled it by accident.
- Run this command to check if it's active:
httpd -M | grep rewrite - If you don't see
rewrite_module (shared)in the output, enable it:- For RHEL/CentOS (common on AWS EC2):
sudo ln -s /etc/httpd/mods-available/rewrite.load /etc/httpd/mods-enabled/rewrite.load - For Debian/Ubuntu:
sudo a2enmod rewrite
- For RHEL/CentOS (common on AWS EC2):
- Restart Apache after enabling:
sudo systemctl restart httpd
Even if mod_rewrite is on, your site's .htaccess file won't work unless Apache allows it via the AllowOverride directive.
- Open your site's Apache config file (usually at
/etc/httpd/conf.d/your-site.confor/etc/httpd/httpd.conf) - Find the
<Directory>block pointing to your web root (like/var/www/html). Update it to look like this:<Directory /var/www/html> Options Indexes FollowSymLinks AllowOverride All Require all granted </Directory> - Save the file and restart Apache:
sudo systemctl restart httpd
Sometimes the upgrade process can overwrite or break your .htaccess rules, even if PHP 7.0 itself doesn't conflict with them.
- First, back up your current file:
sudo cp /var/www/html/.htaccess /var/www/html/.htaccess.bak - If you're using a CMS like WordPress, re-save your permalink settings (Settings > Permalinks) to generate a fresh, compatible
.htaccess - For custom apps, make sure your rewrite rules are correct. A standard rule for routing all requests to
index.php(common in MVC frameworks) looks like this:RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 [L]
If you switched to PHP-FPM during the upgrade (a common setup for PHP 7.0), misconfiguration here can break routing.
- Check your Apache config to ensure it's passing PHP requests to PHP-FPM. Look for lines like:
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$1 - Confirm the port (9000 is default) matches what PHP-FPM uses. You can check this in
/etc/php-fpm.d/www.confunder thelistendirective. - Restart both services:
sudo systemctl restart php-fpm && sudo systemctl restart httpd
If none of the above works, the error logs will tell you exactly what's going wrong.
- View the log in real-time:
sudo tail -f /var/log/httpd/error_log - Try accessing a broken page, then look for new entries. You might see permission issues, missing modules, or invalid rewrite rules that aren't obvious at first glance.
After working through these steps, your site's routing should be back to normal. If you run into specific issues with any of these steps, feel free to share more details!
内容的提问来源于stack exchange,提问作者Akhilesh Sharma

