如何使用 .htaccess 删除 URL 的一部分

How can I remove part of URL using .htaccess

我有一个 link https://example.com/src/index.php,并希望 index.phpsrc 文件夹中的其他页面无需 src 即可访问在 URL.

我的 .htaccess 文件:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]
RewriteRule ^([^\.]+)$ .html [NC,L]

您可以使用根 .htaccess 文件中的 mod_rewrite 来执行此操作。例如:

# Serve file from "/src" subdirectory if it exists
RewriteCond %{DOCUMENT_ROOT}/src/[=10=] -f
RewriteRule ^.+\.\w{2,4}$ src/[=10=] [L]

如果您请求 /index.php 并且此文件存在于 /src/index.php 中,那么它将在内部将请求重写到该子目录。

正则表达式 ^.+\.\w{2,4}$ 仅匹配文件(即包含文件扩展名的 URL)。

[=15=] 是对与 RewriteRule 模式 .

匹配的 URL-path 的反向引用

如果一个文件碰巧在两个地方都存在,那么 /src 子目录中的文件将优先。