使用 Apache 和 htaccess 将旧域重定向到新域时出现问题

发布于 2024-07-25 10:22:40 字数 604 浏览 4 评论 0原文

我的主页位于 www.nazgulled.net,我购买了一个新域名 www.ricardoamaral.net。 这两个域都指向同一台服务器。

我在重定向方面遇到两个问题,我当前的代码是这样的:

RewriteCond %{HTTP_HOST} ^(www\.)?nazgulled\.net [NC]
RewriteRule ^(.*)$ http://www.ricardoamaral.net/$1 [L]

现在我只是在测试,但想法是当我准备好移动整个东西时将 [L] 替换为 [L,R=301] ,只是让你知道。

无论如何...

1) 这不起作用,当我尝试它时,我无法访问“nazgulled.net”,它给了我一个“服务器内部错误”,我不明白为什么...我不明白为什么,因为如果用“google.com”替换“ricardoamaral.net”,重定向就可以正常工作:/

2) 我有一些子域,我想也重定向其中的所有内容。 我的第一个选择是为每个子域添加不同的重写条件/规则,但这需要大量的手动代码,如果用户键入一些不存在的子域,他们不会重定向它,我也想要这样。

My homepage is located at www.nazgulled.net and I bought a new domain which is www.ricardoamaral.net. Both these domains point to the same exact server.

I'm having two problems with the redirection, my current code is this:

RewriteCond %{HTTP_HOST} ^(www\.)?nazgulled\.net [NC]
RewriteRule ^(.*)$ http://www.ricardoamaral.net/$1 [L]

For now I'm just testing but the idea is to replace [L] with [L,R=301] when I'm ready to move the whole thing, just so you know.

Anyway...

1) This is not working, when I try it, I can't access "nazgulled.net", it gives me a "server internal error" and I don't understand why... I don't understand why because if replace "ricardoamaral.net" by "google.com", the redirect works just fine :/

2) I have a few subdomains and I would like to redirect everything in those too. My first choice is to add different rewrite conditions/rules for each of the subdomains but that takes a lot of manual code and if the user types some subdomain that doesn't exist, they don't be redirect it and I also want that.

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

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

发布评论

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

评论(3

红尘作伴 2024-08-01 10:22:40

我认为对于您的情况,创建单独的虚拟主机将是理想的选择。 例如,我经常做的是将被重定向的域放置在同一服务器上但在不同的文件夹中,以保持 www 文件夹干净。 我的主站点将在这里:

/var/www/example.com/public

对于我的所有重定向,我将像这样放置它们:

/var/www/redirects/example-2.com/public

在重定向站点的每个公共文件夹中,您可以将此行添加到您的 .htaccess 文件中

RedirectMatch permanent /.* http://example.com/

I think for your situation creating separate vhosts would be the ideal fit. What I do often is place domains that are being redirected on the same server but in a different folder to keep the www folder clean, for instance. My main site would be here:

/var/www/example.com/public

For all my redirects I would place them like so:

/var/www/redirects/example-2.com/public

Within each public folder of the redirected sites you would add this line to your .htaccess file

RedirectMatch permanent /.* http://example.com/
桃扇骨 2024-08-01 10:22:40

对于 1,您可以使用类似的内容来代替:

<VirtualHost *:80>
  ServerAdmin [email protected]
  ServerName nazgulled.net
  ServerAlias www.nazgulled.net
  Redirect permanent / http://www.ricardoamaral.net/
</VirtualHost>

我使用类似的配置,它工作完美,保留查询参数、路径等。

关于您自己的重写规则,我想指出您可能会丢失如果您不包含 QSA,则为您的查询参数。

要诊断内部服务器错误,只需查看虚拟主机中的 ErrorLog 指令或全局日志文件(具体取决于您的配置)提供的日志文件。 它应该告诉您错误的确切原因。

For 1, you can use something like this instead:

<VirtualHost *:80>
  ServerAdmin [email protected]
  ServerName nazgulled.net
  ServerAlias www.nazgulled.net
  Redirect permanent / http://www.ricardoamaral.net/
</VirtualHost>

I use a similar configuration and it works perfectly, keeping query parameters, paths, etc.

Regarding your own rewrite rule, I want to note that you may lose your query params if you don't include QSA.

To diagnose internal server errors, just have a look at the log file provided by your ErrorLog directive within your vhost, or a global one, depending on your configuration. It should give you the exact reason for the error.

国产ˉ祖宗 2024-08-01 10:22:40

这是最好的解决方案。 创建一个记事本文件并将其另存为 .htaccess(如果您还没有现有记事本文件)。
使用以下代码更新 .htaccess 文件并保存

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]

This is best solutions. Create a notepad file and save it as .htaccess if you do not already have an existing one.
Update the .htaccess file with the following code and save

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://new-domain.com/$1 [R=301,L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文