You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.php points to http://localhost/index.php, but your actual index file lives at http://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 after http://localhost/scripts/mysite/ into the $1 variable (so "home" in your example becomes $1).
    • index.php?page=$1: Uses a relative path to point to your local index file (stays within the mysite directory) and passes the captured value as the page parameter.
    • [QSA]: Preserves any extra query parameters you might add (e.g., http://localhost/scripts/mysite/home?foo=bar becomes index.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:

  1. Open the XAMPP Control Panel, click "Config" next to Apache, then select "httpd.conf".
  2. Find the line #LoadModule rewrite_module modules/mod_rewrite.so and remove the # to uncomment it.
  3. Save httpd.conf and 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:17:20