需删除htaccess中page.php?url=参数,URL重写遇403禁止访问问题
Hey there, let’s work through that 403 Forbidden error you’re getting when trying to clean up your news portal’s URLs. I’ve troubleshooted plenty of Apache rewrite issues like this, so here’s a step-by-step fix to get your pretty URLs working:
First up, make sure the mod_rewrite module is turned on—this is required for URL rewriting to work at all:
- For Linux/Apache environments, run these commands in your terminal:
sudo a2enmod rewrite sudo systemctl restart apache2 - If you’re using XAMPP/WAMP on Windows, open your
httpd.conffile, find the line#LoadModule rewrite_module modules/mod_rewrite.so, remove the leading#, then restart your Apache server.
In your project_07_news_portal directory, create or update the .htaccess file with these rules. This will map your clean URLs to the original page?url=... structure:
RewriteEngine On # Don't rewrite actual files or directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Forward clean URLs to your page handler (adjust to page.php if needed) RewriteRule ^(.*)$ page?url=$1 [L,QSA]
Pro tip: If your handler file is named page.php (not just page), make sure to include the .php suffix in the rule—missing this is a common cause of 403 errors.
Apache blocks .htaccess changes by default for security. You’ll need to update your site’s Apache configuration (like 000-default.conf or your custom virtual host file) to allow overrides in your project directory:
<Directory /var/www/html/project_07_news_portal> Options Indexes FollowSymLinks MultiViews AllowOverride All Require all granted </Directory>
After making this change, restart Apache again—this is the #1 reason people hit 403s with rewrite rules.
Incorrect permissions can trigger Apache’s security restrictions:
- Set your
.htaccessfile to644(owner read/write, others read-only):chmod 644 /var/www/html/project_07_news_portal/.htaccess - Set your project directory to
755:chmod 755 /var/www/html/project_07_news_portal
Avoid using 777 permissions—Apache will reject these as insecure, leading to a 403.
Once everything’s configured, visit project_07_news_portal/altay_tanki_ihalesi_icin_geri_sayim and check if your PHP code can access $_GET['url'] correctly. If you still get errors, check Apache’s error log (usually /var/log/apache2/error.log on Linux) for specific details—it’ll tell you exactly whether the issue is with your rewrite rules, permissions, or something else.
内容的提问来源于stack exchange,提问作者John F.

