301 使用通配符和 .htaccess 重定向


301 Redirect Using Wildcards and .htaccess

我是使用 301 重定向的新手,所以我不知道这是否可能,但我想获取旧 url 的一部分并使用通配符在新 url 中使用它。

example.com/folder/file.php 至

example.com/newfolder/newfolder/file-keyword/

其中文件名是通配符,文件名(不带.php)插入到新 url 中 example.com/newfolder/newfolder/{在此处插入文件名}-关键字/

您可以使用重写规则来完成此操作。例如:

# 如果它在你的 [docRoot]/folder/.htaccess 中:
重写规则 ^(.+)''.php http://example.com/newfolder/newfolder/$1-keyword/

# 如果它进入你的 [docRoot]/.htaccess:
重写规则 ^文件夹/(.+)''.php http://example.com/newfolder/newfolder/$1-keyword/

# 如果它进入您的主配置:
重写规则 ^/文件夹/(.+)''.php http://example.com/newfolder/newfolder/$1-keyword/

这假设您的重写模块加载在主配置中:
负载模块rewrite_module模块/mod_rewrite.so

并在声明重写规则的同一范围内启用:
重写引擎打开