我的.htaccess规则失效,子路径访问异常求技术支持
Let's break down why your subpath requests (like http://my_site_name.com/other_p...) are failing even though the root URL works, and walk through actionable fixes:
1. Fix Apache's AllowOverride Setting (Most Likely Culprit)
Your virtual host might be blocking .htaccess from applying rewrite rules. Check your virtual host config file (usually located at /etc/apache2/sites-available/my_site_name.com.conf) for the <Directory> block targeting your public folder:
<Directory /var/www/html/my_site_name.com/public_html> AllowOverride All # Must be set to All or FileInfo to enable rewrites Require all granted </Directory>
If AllowOverride is set to None or lacks FileInfo, Apache will ignore your .htaccess rules entirely. After updating, restart Apache to apply changes:
sudo systemctl restart apache2
2. Adjust the RewriteRule Format
Some PHP environments (especially CGI/FastCGI setups) don't handle the index.php/$1 path structure well. Try modifying your rule to pass the path as a query string instead:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?path=$1 [L,QSA] </IfModule>
The QSA flag ensures any existing query strings are preserved alongside the new path parameter. You'll need to update your PHP code to read the requested path from $_GET['path'] instead of relying on PATH_INFO.
If you prefer to keep using PATH_INFO, confirm your php.ini has cgi.fix_pathinfo set to 1 (use phpinfo() to check this setting).
3. Confirm mod_rewrite is Enabled
Even with the <IfModule> wrapper, the rewrite module might not be active on your server. Enable it with these commands:
sudo a2enmod rewrite sudo systemctl restart apache2
4. Rule Out Caching or Interfering Files
- Try accessing the problematic subpath in incognito mode to bypass browser caching.
- Check if there are other
.htaccessfiles in parent directories (like/var/www/html) that might be overriding your rules.
5. Debug with Rewrite Logs
If you're still stuck, enable Apache's rewrite log to see exactly what's happening with each request. Add this to your virtual host config temporarily:
RewriteLog /var/log/apache2/rewrite.log RewriteLogLevel 3
Restart Apache, make a test request to the subpath, then check the log file for detailed rewrite steps. Remember to disable this after debugging—it can fill up logs quickly.
内容的提问来源于stack exchange,提问作者vanilla-query

