简体   繁体   English

如何使用.htaccess将example.com.tw重定向到example.com?

[英]How to use .htaccess to redirect example.com.tw to example.com?

Summary 摘要

I have bought two domains, both 'example.com.tw' and 'example.com'. 我已经购买了两个域名,分别是“ example.com.tw”和“ example.com”。 I hope that when a user input 'example.com.tw' in the address bar, it can redirect to 'example.com' automatically. 我希望当用户在地址栏中输入“ example.com.tw”时,它可以自动重定向到“ example.com”。 Below is my try. 下面是我的尝试。

My try 我的尝试

RewriteCond %{HTTP_HOST} ^www\.example\.com\.tw [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L,NE]

Questions 问题

  1. Is it correct above? 上面正确吗?
  2. Is there any better way to improve the code above? 有没有更好的方法来改进上面的代码?

The issue is that when a user input the subdomain (Such as: 'news.example.com.tw'), will it redirect correctly? 问题是,当用户输入子域(例如:“ news.example.com.tw”)时,它会正确重定向吗? (Will it redirect to 'news.example.com' successfully?) (它将成功重定向到“ news.example.com”吗?)

To remove the .tw TLD: 删除.tw TLD:

Assuming both domains point to the same place and any subdomains are simply in subdirectories off the main domains document root (or point to the document root of the main domain itself). 假设两个域都指向同一位置,并且任何子域都位于主域文档根目录之外的子目录中(或指向主域本身的文档根目录)。 Then, in your root .htaccess file: 然后,在您的根.htaccess文件中:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.tw$
RewriteRule ^ http://%1%{REQUEST_URI} [R=302,L]

Replace 302 (temporary) with 301 (permanent) when you are sure it's working OK. 确定可以正常运行时,请用301 (永久)替换302 (临时)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用,htaccess将example.com?id=12重定向到example.com/index.php?id=12 - using ,htaccess redirect example.com?id=12 to example.com/index.php?id=12 将example.com重定向到example.com/blog - Redirect example.com to example.com/blog 我应使用哪种方法将example.com定向到www.example.com-重定向.htaccess或DNS记录? - Which approach I should use to direct example.com to www.example.com - redirect .htaccess or DNS record? htaccess https://example.com/dash/something 链接重定向问题 - htaccess https://example.com/dash/something link redirect problem .htaccess用于分页以重定向example.com/page/page_id - .htaccess for pagination to redirect example.com/page/page_id 如何让所有 example.com 页面重定向到 example.com/smth - How can I make all example.com pages redirect to example.com/smth 如何将example.com/forum/index.php重定向到example.com/forum/ - How to redirect example.com/forum/index.php to example.com/forum/ 将www.domain.com/example.com重定向到example.com - redirect www.domain.com/example.com to example.com 如何使用.htaccess将http://example.com/test.php设置为http://example.com/index.php?do=test? - How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess? 我如何将域(例如 example.com)重定向到 AWS Route53 中的子目录 example.com/data/ - How do i redirect domain (e.g. example.com) to subdirectory example.com/data/ in AWS Route53
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM