一、Linux主机安装WordPress进行301重定向:
wordpress后台设置固定链接时,提示放置的.htaccess文件如下:
Options +FollowSymLinks RewriteEngine On RewriteBase / RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
利用.htaccess文件把带www的301重定向到不带www的,根目录下.htaccess文件写法:
Options +FollowSymLinks RewriteEngine On RewriteBase / #从这里开始插入Start RewriteCond %{HTTP_HOST} ^website_name [NC] RewriteRule ^(.*)$ /$1 [L,R=301] #在这里结束插入End RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]
如果有多个域名需要重定向,上面插入的地方这样写:
RewriteCond %{HTTP_HOST} ^old.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.old.com [NC,OR] RewriteCond %{HTTP_HOST} ^www.xxx.com [NC] RewriteRule ^(.*)$ https://xxx.com/$1 [L,R=301]
二、Windows主机安装WordPres301重定向:
wordpress后台设置固定链接时,提示放置的web.config文件如下:
<configuration> <system.webServer> <rewrite> <rules> <rule name="wordpress" patternSyntax="Wildcard"> <match url="*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
利用web.config文件把带www的301重定向到不带www的,根目录下web.config文件写法:
<configuration> <system.webServer> <rewrite> <rules> <rule name="wordpress" patternSyntax="Wildcard"> <match url="*" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php" /> </rule> <rule name="WWW Redirect" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^website_name" /> </conditions> <action type="Redirect" url="/{R:0}" redirectType="Permanent" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
实际上就是在原来的最后一个
</rules>
之前一行添加如下代码:
<rule name="WWW Redirect" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^website_name$" /> </conditions> <action type="Redirect" url="/{R:0}" redirectType="Permanent" /> </rule>
如果有多个域名需要重定向,则上面添加的代码这样写:
<rule name="301 Redirect 1" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^website_name" /> </conditions> <action type="Redirect" url="/{R:0}" redirectType="Permanent" /> </rule> <rule name="301 Redirect 2" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions logicalGrouping="MatchAll"> <add input="{HTTP_HOST}" pattern="^website_name" /> </conditions> <action type="Redirect" url="/{R:1}" redirectType="Permanent" /> </rule>
不适用于nginx的服务器
@角落
用于Apache的
哥们,可以么?我的是win主机 IIS 啊 !
前面用了别人介绍的 404 页面 也不起作用! 你这个貌似行不 ?
@小孙博客
哥们,无效的东西我写了干嘛?我现在的主机就是Windows+IIS7,以前用的是Linux+Apache
我的也是IIS7,用了博主的规则,带WWW。域名。COM/889类似的链接怎么跳到不带WWW首页啦?
@孤风
可能你的主机不支持用web.config文件来配置