XAMPP环境下Apache URL重写规则配置失败求助
Hey Janice, let's fix that URL rewrite issue you're having. The problem with your current .htaccess rules is two-fold—your conditions are backwards, and the target path is pointing to the wrong location. Let's break this down step by step:
What's Wrong With Your Current Rules?
- The RewriteCond statements are inverted: Right now, you're telling Apache to only apply the rewrite if the requested path is an existing file and an existing directory (which is impossible for a single path). We need to rewrite the URL only when the requested path is not a real file or directory.
- The target path uses an absolute root slash:
/index.phppoints tohttp://localhost/index.php, but your actual index file lives athttp://localhost/scripts/mysite/index.php. Using the absolute slash here pulls you out of your site's directory, which is why you're seeing the "Object not found" error.
Fixed .htaccess Rules
Replace your current code with this, and save it in C:\xampp\htdocs\scripts\mysite\.htaccess:
RewriteEngine On # Skip rewrite if the request is for a real file or directory RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # Rewrite clean URL to the actual index.php with page parameter RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]
Let's Explain Each Part
RewriteEngine On: Enables Apache's URL rewriting module (you had this part right already).RewriteCond %{REQUEST_FILENAME} !-f: Skips the rewrite if the requested path matches a real file (like CSS, JS, or image files—we don't want to break those).RewriteCond %{REQUEST_FILENAME} !-d: Skips the rewrite if the requested path matches a real directory.RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]:^(.*)$: Captures everything afterhttp://localhost/scripts/mysite/into the$1variable (so "home" in your example becomes$1).index.php?page=$1: Uses a relative path to point to your local index file (stays within themysitedirectory) and passes the captured value as thepageparameter.[QSA]: Preserves any extra query parameters you might add (e.g.,http://localhost/scripts/mysite/home?foo=barbecomesindex.php?page=home&foo=bar).[L]: Stops processing further rewrite rules after this one, preventing unexpected conflicts.
Quick Setup Check
Before testing, make sure mod_rewrite is enabled in XAMPP:
- Open the XAMPP Control Panel, click "Config" next to Apache, then select "httpd.conf".
- Find the line
#LoadModule rewrite_module modules/mod_rewrite.soand remove the#to uncomment it. - Save
httpd.confand restart Apache.
Now visit http://localhost/scripts/mysite/home—it should load the content from index.php?page=home without showing the query string in the URL.
内容的提问来源于stack exchange,提问作者Janice Zhong
相关产品推荐
相关产品推荐

