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

如何实现访问指定URL时加载目标PHP文件且浏览器URL保持不变?

Hey there! To make your setup work where visiting www.example.com/user/login loads www.example.com/app/system/core/user/UserLogin.php while keeping the original URL in the address bar, you'll want to use URL rewriting—here's how to implement it for the two most common web servers:

Apache (with mod_rewrite)

First, make sure the mod_rewrite module is enabled on your Apache server. Then, create or edit a .htaccess file in your website's root directory and add these rules:

RewriteEngine On

# Skip rewrite if the requested path is an existing file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite the /user/login path to your target PHP file
RewriteRule ^user/login$ /app/system/core/user/UserLogin.php [L]

The [L] flag tells Apache this is the last rule to process, preventing any further rewrite rules from interfering with this request.

Nginx

In your Nginx server configuration block (usually found in /etc/nginx/sites-available/ or similar), add one of these options:

This checks if the requested path exists as a file or directory first, then falls back to your PHP file:

location /user/login {
    try_files $uri $uri/ /app/system/core/user/UserLogin.php?$args;
}

Option 2: Using rewrite directive

If you prefer a direct rewrite rule:

rewrite ^/user/login$ /app/system/core/user/UserLogin.php last;

The last flag makes Nginx restart the location matching process with the rewritten path, ensuring your PHP handler processes the request correctly.

Just remember to restart your web server after making these changes to apply the new configuration!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 07:55:12