大网数据广告活动
网站运营与故障问答

How to redirect URLs with a specific IDs to new URLs on a new domain name?

· 52CE 我爱测

I want to redirect all pages where there is a specific word inside in the URL. Both websites have the same articles. For example: videos.example.com.ph/its-showtime-hashtags-hashtags-and-dawns-popular-dance-moves_966aaafd8.html s…

问题原文

原帖链接 · Aesir John · CC BY-SA 3.0

I want to redirect all pages where there is a specific word inside in the URL. Both websites have the same articles.

For example:

videos.example.com.ph/its-showtime-hashtags-hashtags-and-dawns-popular-dance-moves_966aaafd8.html

should be redirected to

example.com.ph/its-showtime-hashtags-hashtags-and-dawns-popular-dance-moves_8a4521384.html

The difference is the Domain and the ID at the end. All articles with the URL http://videos.example.com.ph and the word its-showtime inside should be redirected to the same article on http://example.com.ph/

If this is too difficult, a redirect to example.com.ph mainpage would be ok too.

已采纳解答

原帖链接 · MrWhite · CC BY-SA 3.0

You can't do a simple redirect of "all articles" if the source and destination IDs in the URL are different and there is no easy way to map between the two. For example, how do we know that 966aaafd8 maps to 8a4521384?

In order to do this you would need to create a RewriteMap in your server config (containing all the source / destination IDs) or rewrite requests to another script that performs the lookup and then redirects.

However, you can redirect all requests to the document root on the other domain if that is OK. But this is generally a bad user experience (and hence bad for SEO), if that is a concern.

At the top of your .htaccess file in the videos.example.com.ph subdomain:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} =videos.example.com.ph [NC]
RewriteRule ^its-showtime http://example.com.ph/ [NC,R=302,L]

If the videos.example.com.ph subdomain and the main domain are physically hosted in at different places then you can remove the RewriteCond directive.

Note that this matches its-showtime at the start of the URL (as in your examples). If you need it to match anywhere in the URL, then just remove the ^ prefix.

Note also that this is a case-insensitive match (NC - NOCASE) - if this is not required then remove the NC flag from the RewriteRule. (The NC flag on the RewriteCond should remain, as this simply catches malformed requests that would otherwise still resolve.)

Change the 302 (temporary) redirect to 301 (permanent) redirect when you are sure it's working OK - assuming this should be a permanent redirect?

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2016-01-03)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。