[英]301 Redirect for All Subdirectories
I am changing domain names for a WordPress site and want to set up a structure like so: 我正在更改WordPress网站的域名,并希望设置如下结构:
If a user accesses olddomain.com/page , he will be re-directed to newdomain.com/page , etc. In other words, only the pre-slash part of the domain would change. 如果用户访问olddomain.com/page ,他将被重定向到newdomain.com/page等。换句话说,仅域名的斜杠前部分会更改。 I want to do this for all pages under the old domain.
我想对旧域下的所有页面执行此操作。
So far, I have only been able to get olddomain.com to re-direct to newdomain.com , but not any of the sub-directories (for example, olddomain.com/page does not re-direct to newdomain.com/page ). 到目前为止,我只能使olddomain.com重定向到newdomain.com ,但是没有任何子目录(例如, olddomain.com/page不能重定向到newdomain.com/page) )。 Here is my .htaccess file:
这是我的.htaccess文件:
# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^newdomain\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
Does anyone know what I did wrong? 有人知道我做错了吗?
You must put all of the redirect rules before your routing rules. 您必须将所有重定向规则放在路由规则之前 。 The wordpress rules route everything to
index.php
, including anything you intend to redirect. wordpress规则将所有内容路由到
index.php
,包括您要重定向的任何内容。 So the redirects must happen before any sort of internal routing happens. 因此,重定向必须在任何类型的内部路由发生之前发生。
Simply put your redirect rule before the wordpress rules: 只需将您的重定向规则放在wordpress规则之前:
# BEGIN GD-SSL
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_USER_AGENT} ^(.+)$
RewriteCond %{SERVER_NAME} ^newdomain\.com$
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
Header add Strict-Transport-Security "max-age=300"
</IfModule>
# END GD-SSL
RewriteEngine On
RewriteCond %{HTTP_HOST} olddomain\.com$ [NC]
RewriteRule ^(.*)$ https://newdomain.com/$1 [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change your .htaccess to the below code on the old domain: 将您的.htaccess更改为旧域上的以下代码:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.olddomain.com$ [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301,L]
</IfModule>
This will redirect the entire website on a page by page basis. 这将逐页重定向整个网站。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.