您可以使用 Apache 中的 mod_rewrite 模块在使用 .htaccess 的 PHP 中重定向 URL。
要重定向特定 URL,您可以在 .htaccess 文件中使用以下语法:
RewriteEngine On
RewriteRule ^old-url$ http://www.example.com/new-url [R=301,L]
这将使用“301”重定向(永久重定向)将对 URL“old-url”的任何请求重定向到“http://www.example.com/new-url”。
您还可以使用正则表达式来匹配 URL 中的模式并进行相应的重定向。例如,如果要将所有以“old-directory”开头的 URL 请求重定向到“new-directory”,可以使用以下方法:
RewriteEngine On
RewriteRule ^old-directory/(.*)$ http://www.example.com/new-directory/$1 [R=301,L]
这将采用任何以“old-directory”开头的 URL 并将其重定向到相同的 URL,但改为以“new-directory”开头。
修改 htaccess 时要小心,因为它可能会导致网站出现故障。在进行任何更改之前进行备份始终是一个好习惯。