重定向停放域名 (htaccess)

发布于 2024-09-08 17:24:46 字数 1841 浏览 1 评论 0原文

我有两个域名,格式为 foo.com 和 foo.net。目前,我的 .htaccess 中有以下内容,可将非 www 重定向到 www:

# Rewrite URL to force WWW
  RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
  RewriteCond %{SERVER_PORT} !=443
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
  RewriteCond %{SERVER_PORT} =443
  RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
  RewriteCond %{SERVER_PORT} !=443
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
  RewriteCond %{SERVER_PORT} =443
  RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

但是,我想将以 .net 结尾的 URL 重定向到 .com,包括子域。 示例:

foo.net-> foo.com
www.foo.net -> www.foo.com
foo.net/bar -> foo.com/bar
子.foo.net -> sub.foo.com
sub.foo.net/bar -> sub.foo.com/bar

我曾经在另一个网站上有这个,但那是几年前的事了,我不再有 .htaccess 了。有人知道这样做的简单方法吗?

编辑:这是 .htaccess 的其余部分:

# BEGIN WordPress
#
  RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
# RewriteBase /
#
# if this request is for "/" or has already been rewritten to WP
  RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
  RewriteCond $1 \.(gif|jpg|png|php|ico|css|js)$ [NC,OR]
# or if URL resolves to existing file
  RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
  RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
  RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
  RewriteRule . /index.php [L]
#
# END wordpress 

# Rewrite URL to force WWW
   -(see top of post)-

I have two domains in the format of foo.com and foo.net. I currently have the following in my .htaccess to redirect non-www to www:

# Rewrite URL to force WWW
  RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
  RewriteCond %{SERVER_PORT} !=443
  RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^[^.]*\.[^.]*$
  RewriteCond %{SERVER_PORT} =443
  RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
  RewriteCond %{SERVER_PORT} !=443
  RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

  RewriteCond %{HTTP_HOST} ^www\.(.*\.[^.]*\.[^.]*)$ [NC]
  RewriteCond %{SERVER_PORT} =443
  RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

However, I want to redirect URLs ending in .net to go to .com, including the subdomains.
Examples:

foo.net -> foo.com
www.foo.net -> www.foo.com
foo.net/bar -> foo.com/bar
sub.foo.net -> sub.foo.com
sub.foo.net/bar -> sub.foo.com/bar

I used to have this on another site, but it was years ago and I don't have the .htaccess anymore. Does anybody know an easy way of doing this?

Edit: Here is the rest of the .htaccess:

# BEGIN WordPress
#
  RewriteEngine on
#
# Unless you have set a different RewriteBase preceding this point,
# you may delete or comment-out the following RewriteBase directive
# RewriteBase /
#
# if this request is for "/" or has already been rewritten to WP
  RewriteCond $1 ^(index\.php)?$ [OR]
# or if request is for image, css, or js file
  RewriteCond $1 \.(gif|jpg|png|php|ico|css|js)$ [NC,OR]
# or if URL resolves to existing file
  RewriteCond %{REQUEST_FILENAME} -f [OR]
# or if URL resolves to existing directory
  RewriteCond %{REQUEST_FILENAME} -d
# then skip the rewrite to WP
  RewriteRule ^(.*)$ - [S=1]
# else rewrite the request to WP
  RewriteRule . /index.php [L]
#
# END wordpress 

# Rewrite URL to force WWW
   -(see top of post)-

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

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

发布评论

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

评论(2

递刀给你 2024-09-15 17:24:46

忽略这是迄今为止我写过的最邪恶的重写规则集这一事实,令人惊讶的是,这似乎在一个漂亮、紧凑的包中照顾了您当前拥有的内容以及您想要的内容。

如果您有任何问题,请告诉我:

RewriteCond %{HTTPS} =on
RewriteRule .* - [E=RW_HTTPS:s]

RewriteCond %{HTTP_HOST}     ^www\.(.*\.[^.]*\.[^.]*)$ [NC,OR]
RewriteCond www.%{HTTP_HOST} ^(www\.[^.]*\.[^.]*)$
RewriteRule .* - [E=RW_THOST:%1]

RewriteCond %{ENV:RW_THOST}  ^(.*)\.(net|com)$         [NC,OR]
RewriteCond %{HTTP_HOST}     ^(.*)\.net$               [NC]
RewriteRule .* http%{ENV:RW_HTTPS}://%1.com%{REQUEST_URI} [R=301,L]

Ignoring the fact that this is by far the most devilish rewrite ruleset that I've ever written, this surprisingly seems to take care of what you currently have, plus what you want, in a nice, compact package.

Let me know if you have any problems with it:

RewriteCond %{HTTPS} =on
RewriteRule .* - [E=RW_HTTPS:s]

RewriteCond %{HTTP_HOST}     ^www\.(.*\.[^.]*\.[^.]*)$ [NC,OR]
RewriteCond www.%{HTTP_HOST} ^(www\.[^.]*\.[^.]*)$
RewriteRule .* - [E=RW_THOST:%1]

RewriteCond %{ENV:RW_THOST}  ^(.*)\.(net|com)$         [NC,OR]
RewriteCond %{HTTP_HOST}     ^(.*)\.net$               [NC]
RewriteRule .* http%{ENV:RW_HTTPS}://%1.com%{REQUEST_URI} [R=301,L]
夕色琉璃 2024-09-15 17:24:46

要将停放域重定向到具有特定 URL 的主网站域,请在 htacess 文件中尝试以下代码:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^parked-domain.com [OR]
RewriteCond %{HTTP_HOST} ^www.parked-domain.com
RewriteRule ^(.*)$ https://main-domain.com/custom-url [L,R=301]

To Redirect Parked domain to main website domain with specific URL Try The following code on your htacess file :

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^parked-domain.com [OR]
RewriteCond %{HTTP_HOST} ^www.parked-domain.com
RewriteRule ^(.*)$ https://main-domain.com/custom-url [L,R=301]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文