具有排除条件的 301 重定向 (htaccess)

Posted

技术标签:

【中文标题】具有排除条件的 301 重定向 (htaccess)【英文标题】:301 Redirects (htaccess) with exclude conditions 【发布时间】:2012-01-23 14:11:09 【问题描述】:

我目前正在将网站(商店)移动到新域,并在其位置放置该网站的非商店版本(用 wordpress 制作)。目的是让大多数 url 重定向到新域,除了新站点的少数页面。我在 *** 上找到了其他帖子,但不幸的是我似乎无法让它工作。任何帮助将不胜感激,谢谢 Jason。

我需要排除的网址是: / (主页) /news 及其帖子 /news/post-name /products 及其帖子 /products/post-name /offers 及其帖子 /offers/post-name /我们的理念 /我们的队伍 /我们的梦想

这是我尝试过的:

<IfModule mod_rewrite.c>

RewriteEngine on

#Home
RewriteCond %REQUEST_URI !^/

#News
RewriteCond %REQUEST_URI !^/news(.*)

#Products
RewriteCond %REQUEST_URI !^/products(.*)

#Offers
RewriteCond %REQUEST_URI !^/offers(.*)

#Philosophy
RewriteCond %REQUEST_URI !^/our-philosophy(.*)

#Team
RewriteCond %REQUEST_URI !^/our-team(.*)

#Dream
RewriteCond %REQUEST_URI !^/our-dream(.*)

RewriteRule (.*) http://www.cotswold-fayre.co.uk/$1 [R=301,L]

</IfModule>



# 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

【问题讨论】:

这应该是服务器故障,与编程无关.. 【参考方案1】:

尝试将以下内容添加到您的 .htaccess 文件中

RewriteEngine on

#Home: Exclude the home Page
RewriteCond %REQUEST_URI !^/$ [OR]

#News: exclude anything that starts with /news, /products etc
RewriteCond %REQUEST_URI !^/(news|products|offers|our-philosophy|our-team|our-dream) [NC]

RewriteRule (.*) http://www.cotswold-fayre.co.uk/$1 [R=301,L]

【讨论】:

以上是关于具有排除条件的 301 重定向 (htaccess)的主要内容,如果未能解决你的问题,请参考以下文章

.htaccess 重定向但排除某些文件夹

使用 .htaccess 重定向网站但排除一个文件夹

Magento 2中的htaccess 301重定向

301 使用 .htaccess 重定向 URL 模式

301 重定向.htaccess

仅针对域的 Htaccess 301 重定向