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

如何使用.htaccess(正则表达式)实现URL搜索替换

.htaccess 正则配置方案

以下是整合了你需求的完整.htaccess代码,新规则已插入到合适位置:

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

# 需求1:清理video路径中的冗余数字段
RewriteRule ^video-([^/]+)/\d+/0/(.+)$ /video-$1/$2 [L,NE,R=301]

# 需求2:移除search页面的查询参数
RewriteCond %{QUERY_STRING} .+
RewriteRule ^search/full\+movie$ /$0? [L,NE,R=301]

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

# 需求3:替换页面内容中的外部链接
<IfModule substitute_module>
    AddOutputFilterByType SUBSTITUTE text/html text/plain text/xml
    Substitute s|https://anothersiteurl.com/search/full\+movie|https://mysiteurl.com/search/full\+movie|ni
</IfModule>

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

各规则说明:

  • 需求1规则:匹配video-xxx/数字/0/xxx格式的路径,移除中间的数字/0冗余段,直接跳转至清理后的URL。\d+匹配任意长度数字,[^/]+精准匹配video后的唯一标识,避免误改其他路径。
  • 需求2规则:当search/full+movie页面带有查询参数时,通过替换路径后加?的方式清空所有查询字符串,实现跳转至无参数的干净URL。
  • 需求3规则:借助mod_substitute模块,在页面输出时自动替换内容中所有指定的外部链接。ni参数表示忽略大小写、全局匹配,确保所有出现的目标链接都被替换。

注意:若服务器未启用mod_substitute模块,需联系主机商开启后,需求3的内容替换功能才能生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 03:15:39