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

redirect various http domains to certain https domain via htaccess

· 52CE 我爱测

I have a bunch of domain names with different TLDs. What I want is to point several domains to the "main"-domain which has a SSL cert (single domain). Like this: https://example.com (main domain with SSL cert) http://example.net …

问题原文

原帖链接 · okiedokey · CC BY-SA 4.0

I have a bunch of domain names with different TLDs. What I want is to point several domains to the "main"-domain which has a SSL cert (single domain).

Like this:

https://example.com (main domain with SSL cert)
http://example.net (should redirect to https://example.com and rewrite the URLs)
http://example.org (should redirect to https://example.com and rewrite the URLs)

The https domain points to the directory which contains the website data. I created a subfolder in that directory where I pointed the additional domains to, because I was thinking I could place a htaccess file there to manage redirection and rewriting. Is that OK so far?

I am having trouble finding the correct htaccess rules which permanently redirect the requested domain (either example.net or example.org) to https://example.com and rewrites the URLs correctly.

I tried the simplest thing that came to my mind:

Redirect permanent / https://example.com/

But that resulted in e.g. https://example.net which, of course, does not work.

Edit:

I use WordPress and put https://example.com as blog URL as well as site URL in the settings and I made WordPress force SSL.

Can someone please help?

已采纳解答

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

With your additional domains pointing to subdirectories off the main domain's document root, you can add the following to the .htaccess in that subdirectory to redirect to the main domain:

RewriteEngine On
RewriteRule (.*) https://maindomain.com/$1 [R=302,L]

Note that this uses mod_rewrite, not mod_alias (Redirect). As such it will override the mod_rewrite directives in the parent .htaccess file. This assumes that the parent .htaccess file (which includes the WordPress directives) does not use mod_alias. (WordPress usually only uses mod_rewrite.)

Change the 302 (temporary) redirect to a 301 (permanent) redirect when you are happy it's working OK.


An alternative (as mentioned in comments) is to simply point these additional domains to the main domain's document root as opposed to subdirectories. Then, you would need to modify the main domains .htaccess file and include a similar external redirect before the WordPress directives. Something like:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?additionaldomain1\.net$
RewriteRule (.*) https://maindomain.com/$1 [R=302,L]

In fact, you could do this anyway with the subdirectories, and not use the .htaccess file in the subdirectory. .htaccess files are inherited by default.

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

资料来源与适用范围

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

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

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