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

如何重定向至index.php且地址栏不显示index.php?

Fixing Index.php Display in Address Bar When Redirecting to Homepage

Hey there! Let's sort out that URL issue where your address bar shows index.php instead of just your root domain when users land on your homepage. Here are the most reliable methods depending on your server setup:

Apache Server (Using .htaccess)

If you're running Apache, this is the go-to approach. First, make sure mod_rewrite is enabled on your server (most hosting providers have this turned on by default). Then create or edit the .htaccess file in your website's root folder and add these rules:

RewriteEngine On

# Permanently redirect any direct requests for index.php to your root domain
RewriteRule ^index\.php$ / [R=301,L]

# Internally route root domain requests to index.php (so content loads without showing the filename)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^$ index.php [L]
  • The R=301 is a permanent redirect, which helps with SEO by telling search engines the old index.php URL should be replaced with the root domain. Use R=302 instead if you only need a temporary redirect.
  • The last two rules ensure that when someone visits your root domain, the server silently loads index.php in the background without changing what's shown in the address bar.

Nginx Server

For Nginx users, you'll need to edit your server block configuration (usually located in /etc/nginx/sites-available/your-domain-name). Add these lines inside the server block:

# Redirect requests for index.php to the root domain permanently
rewrite ^/index\.php$ / permanent;

# Serve index.php when the root domain is accessed
location / {
    try_files $uri $uri/ /index.php?$args;
}

After making changes, test your config with nginx -t to catch any syntax errors, then restart Nginx with sudo systemctl restart nginx for the changes to take effect.

Quick Frontend Fix

Don't forget to update your links to point to the root domain instead of index.php! Replace links like:

<a href="/index.php">Home</a>

With:

<a href="/">Home</a>

This ensures users never click a link that directly points to index.php in the first place, working hand-in-hand with the server rules above.

Important Notes

  • Always back up your configuration files before making changes, just in case something goes wrong.
  • If your index.php is in a subdirectory (not the root), adjust the paths in the rewrite rules to match its location.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 05:08:56