首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何组合Apache重定向?

如何组合Apache重定向?
EN

Stack Overflow用户
提问于 2012-12-06 08:52:53
回答 2查看 762关注 0票数 2

我有一个Apache配置,它具有多个重写规则和重定向,以便获得最可爱的URL,防止SEO的重复,等等。下面是一个示例代码片段(它的功能更多):

代码语言:javascript
复制
# Redirecting non-www to www 
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Removing index.php
RewriteCond %{REQUEST_URI} index\.php$ [NC]
RewriteRule .* / [R=301,L]

# A big bunch of these
Redirect permanent /old-article.html  http://www.example.com/new-article.html
Redirect permanent /another-old-article.html  http://www.example.com/new-article2.html

这工作得很好,但它碰巧生成了很多重定向。常见的情况如下所示:

代码语言:javascript
复制
http://example.com/index.php, 301 redirect to http://www.example.com/index.php
http://www.example.com/index.php, 301 redirect to http://www.example.com

有时会达到4-5个重定向。

现在,我希望所有这些规则都链接在一起,只生成一个301重定向,如下所示:

代码语言:javascript
复制
http://example.com/index.php, 301 redirect to http://www.example.com

我知道我可以花一个下午的时间来思考和排序规则,以获得更好的匹配,而且我还可以创建组合规则。但这会使本已很长的文件变得复杂。我想要一个标志,操作数或任何东西,它将执行所有的规则,就好像他们是内部的,只有当它抓取了每个规则时才发出重定向。这有可能吗?

EN

回答 2

Stack Overflow用户

发布于 2012-12-06 09:43:41

似乎简单地重新排序就能得到你想要的东西:

代码语言:javascript
复制
# A big bunch of these
Redirect permanent /old-article.html  http://www.example.com/new-article.html
Redirect permanent /another-old-article.html  http://www.example.com/new-article2.html

# Removing index.php
RewriteRule ^/index.php http://www.example.com/ [R=301,L]

# Redirecting non-www to www 
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

example.com域名上直接请求一篇旧文章:

代码语言:javascript
复制
http://example.com/old-article.html

将导致一个重定向至:

代码语言:javascript
复制
http://www.example.com/new-article.html

http://example.com/index.phphttp://www.example.com/index.php的请求将导致单个重定向到:

代码语言:javascript
复制
http://www.example.com/

与其他任何内容都不匹配的请求将导致从以下位置重定向:

代码语言:javascript
复制
http://example.com/foo

至:

代码语言:javascript
复制
http://www.example.com/foo

这似乎涵盖了所有的基础。我错过了什么吗?

票数 0
EN

Stack Overflow用户

发布于 2012-12-06 10:03:19

从您的RewriteRule中删除[L]标志,它们将自动合并。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13735230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档