简体   繁体   English

将旧域的每个页面重定向到新域上的相同页面URL

[英]Redirect Each Page of Old Domain to Same Page URL on the New Domain

I know this question has been asked with all variations but I still cannot find the exact answer. 我知道这个问题已被提出所有变化,但我仍然找不到确切的答案。 I have changed my domain name. 我已经更改了我的域名。 All website files and urls are still the same. 所有网站文件和网址仍然相同。 old domain still points so the same server and I have the following code in htaccess file. 旧域仍然指向相同的服务器,我在htaccess文件中有以下代码。

 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
 RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
 RewriteRule ^(.*)$ "https\:\/\/www\.newdomain\.com\/$1" [R=301,L]

Problem is that this only redirects the old domain home page. 问题是,这只会重定向旧域主页。 What can I do so I don't have to add individual redirects for each page. 我能做什么,所以我不必为每个页面添加单独的重定向。

It sounds like you've put these directives in the wrong place in your .htaccess file. 听起来你已将这些指令放在.htaccess文件中的错误位置。 They need to go at the beginning of the file, not at the end. 他们需要在文件的开头,而不是最后。

These directives look typical of cPanel generated redirects. 这些指令看起来是典型的cPanel生成的重定向。 cPanel always places redirects at the end of the .htaccess file (which is often incorrect). cPanel总是将重定向放在.htaccess文件的末尾(这通常是不正确的)。

If you have a front-controller before this (such as that used by WordPress) then you will find that these directives will only get processed for requests to the document root (ie. the "homepage"), as all other requests will be stopped by the front-controller. 如果您之前有一个前端控制器(例如WordPress使用的那个),那么您会发现只有处理对文档根目录(即“主页”)的请求才能处理这些指令,因为所有其他请求都将被停止由前端控制器。

These directives can also be simplified: 这些指令也可以简化:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com [NC]
RewriteRule (.*) https://www.newdomain.com/$1 [R=301,L]

I've removed the $ from the end of the CondPattern so as to match a FQDN (that ends in a dot). 我已经从CondPattern的末尾删除了$以匹配FQDN(以点结尾)。

There is also no need to backslash escape colons, slashes and dots in the RewriteRule substitution , as these characters carry no special meaning here (it's not a regex). RewriteRule 替换中也没有必要使用反斜杠转义冒号,斜杠和点,因为这些字符在这里没有特殊含义(它不是正则表达式)。 (This unnecessary escaping is very typical of cPanel.) (这种不必要的转义是cPanel非常典型的。)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM