问题原文
原帖链接 · 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?
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
