Apache2重定向配置问题:排除index.html的域名跳转需求
Fixing Apache Redirect Rule for test.net (Excluding index.html)
Got it, let's straighten out your Apache rewrite config. The current setup isn't working for two key reasons:
- Your
RewriteRulepattern includes the full URL (http://test.net/), but Apache'sRewriteRuleonly matches the path portion of the request (not the protocol or domain). - The
[^index\.html]syntax is a character class, which matches a single character that isn't in that set of characters—not the entire/index.htmlpath you want to exclude.
Here's the corrected configuration that does exactly what you need:
RewriteEngine On # Exclude requests for /index.html (case-insensitive) RewriteCond %{REQUEST_URI} !^/index\.html$ [NC] # Target only requests to test.net RewriteCond %{HTTP_HOST} ^test\.net$ [NC] # Redirect all matching paths to test.test.net RewriteRule ^(.*)$ http://test.test.net/$1 [R=301,L]
Let's break down each part:
RewriteCond %{REQUEST_URI} !^/index\.html$ [NC]:- The
!negates the condition, so this means "if the request URI is NOT exactly/index.html". [NC]makes the match case-insensitive, so/INDEX.HTMLor/Index.Htmlwill also be excluded.
- The
RewriteCond %{HTTP_HOST} ^test\.net$ [NC]:- Ensures we only apply this rule to requests where the host is
test.net(again, case-insensitive).
- Ensures we only apply this rule to requests where the host is
RewriteRule ^(.*)$ http://test.test.net/$1 [R=301,L]:^(.*)$matches any path (empty or with content) after the domain.$1captures that matched path and inserts it into the target URL, sotest.net/xxxbecomestest.test.net/xxx.[R=301]sends a permanent redirect (good for SEO; useR=302if you need a temporary redirect).[L]tells Apache to stop processing further rewrite rules once this one runs.
Bonus: Exclude the root domain (if it defaults to index.html)
If visiting test.net/ (no path) automatically serves index.html and you want to exclude that too, update the first condition to:
RewriteCond %{REQUEST_URI} !^(/index\.html)?$ [NC]
This matches both the empty root path (/) and /index.html, so neither will be redirected.
内容的提问来源于stack exchange,提问作者min.g
相关产品推荐
相关产品推荐

