正则表达式通过 htaccess 将 url 重定向到新域

发布于 2025-01-13 18:31:23 字数 1041 浏览 0 评论 0原文

我正在尝试通过 htaccess 将具有特定单词的所有 url 重定向到新域。 以下是几个 url 结构:

http://birmingham-printer-repairs.co.uk/brother/printer/repairs/in/abbots-langley/
http://birmingham-printer-repairs.co.uk/brother/printer/repairs/in/accrington/
http://birmingham-printer-repairs.co.uk/sharp/printer/repairs/in/york/

我需要将所有包含 (word Printer/repairs/in) 的 url 重定向到新域

这是我的 WordPress htaccess

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^\/printer\/repairs\/in(.*)$ http://www.newdomain.com/ [R=301,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

I am trying to redirect all url with specific word to a new domain via htaccess.
Here are the few url structure:

http://birmingham-printer-repairs.co.uk/brother/printer/repairs/in/abbots-langley/
http://birmingham-printer-repairs.co.uk/brother/printer/repairs/in/accrington/
http://birmingham-printer-repairs.co.uk/sharp/printer/repairs/in/york/

I need all url that contains (word printer/repairs/in) should be redirected to new domain

Here is my htaccess of wordpress

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^\/printer\/repairs\/in(.*)$ http://www.newdomain.com/ [R=301,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

潇烟暮雨 2025-01-20 18:31:24

您可以使用以下命令:

记住:这应该位于 htaccess 的顶部或其他指令之前

RewriteEngine On

RewriteRule printer/repairs/in https://example.com [L,R]

如果两个域(新的和旧的)都指向同一文档根目录,则使用此命令

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule printer/repairs/in https://example.com [L,R]

You can use the following :

Remember : this should be at the top of your htaccess or before other directives

RewriteEngine On

RewriteRule printer/repairs/in https://example.com [L,R]

If both domains (new and old) are pointing to the same document root , then use this

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$
RewriteRule printer/repairs/in https://example.com [L,R]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文