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

如何通过.htaccess实现主页重定向并隐藏URL中的index.html

Hey there, let's walk through these two .htaccess scenarios you're asking about—they're super common, so I'll keep it clear and actionable.

1. How to Implement Homepage Redirects with .htaccess

Homepage redirects usually fall into two common use cases, so I'll cover both:

Redirect non-www homepage to www version

If you want visitors who land on example.com to be sent to www.example.com (critical for SEO and brand consistency), add this to your .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^$ https://www.example.com/ [R=301,L]
  • RewriteEngine On enables Apache's rewrite module (make sure this is enabled on your server first!)
  • RewriteCond checks if the incoming domain is exactly example.com (the [NC] flag makes it case-insensitive)
  • RewriteRule ^$ targets the homepage (empty path), R=301 is a permanent redirect (best for SEO), and L tells Apache this is the last rule to process for this request.

Redirect homepage to a different internal page

If you want the root domain to point to something like /home instead of the default index file, use this:

RewriteEngine On
RewriteRule ^$ /home [R=301,L]

This directly routes any request to www.example.com/ to www.example.com/home with a permanent redirect.

2. Make URLs Show www.example.com Instead of www.example.com/index.html

This involves two key steps: redirecting users who type in index.html to the clean root URL, and making sure the server still loads index.html when the root is accessed (without showing it in the URL). Here's the code:

RewriteEngine On
# Redirect visitors who explicitly type index.html to the root
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/
RewriteRule ^index\.html$ / [R=301,L]
# Internally map the root URL to index.html (users won't see this change)
RewriteRule ^$ /index.html [L]
  • The first rule uses %{THE_REQUEST} to check the original request line (like GET /index.html HTTP/1.1), which prevents redirect loops that can happen with simpler rules. It sends anyone accessing index.html to the clean root URL.
  • The second rule is an internal rewrite: when someone visits www.example.com/, the server quietly loads index.html in the background, but the URL stays clean and user-friendly.

Quick Notes:

  • Always test with a 302 temporary redirect first if you're experimenting, then switch to 301 once you're sure it's working (301s are cached by browsers and search engines, so you want to avoid mistakes here).
  • Clear your browser cache after modifying .htaccess—old redirects can stick around and confuse your testing.
  • Double-check that mod_rewrite is enabled on your Apache server. Most shared hosts have it on by default, but if not, you might need to contact your host or enable it via your control panel.

内容的提问来源于stack exchange,提问作者Kyle

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 08:03:27