我有一个Apache配置,它具有多个重写规则和重定向,以便获得最可爱的URL,防止SEO的重复,等等。下面是一个示例代码片段(它的功能更多):
# 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这工作得很好,但它碰巧生成了很多重定向。常见的情况如下所示:
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重定向,如下所示:
http://example.com/index.php, 301 redirect to http://www.example.com我知道我可以花一个下午的时间来思考和排序规则,以获得更好的匹配,而且我还可以创建组合规则。但这会使本已很长的文件变得复杂。我想要一个标志,操作数或任何东西,它将执行所有的规则,就好像他们是内部的,只有当它抓取了每个规则时才发出重定向。这有可能吗?
发布于 2012-12-06 09:43:41
似乎简单地重新排序就能得到你想要的东西:
# 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域名上直接请求一篇旧文章:
http://example.com/old-article.html将导致一个重定向至:
http://www.example.com/new-article.html对http://example.com/index.php或http://www.example.com/index.php的请求将导致单个重定向到:
http://www.example.com/与其他任何内容都不匹配的请求将导致从以下位置重定向:
http://example.com/foo至:
http://www.example.com/foo这似乎涵盖了所有的基础。我错过了什么吗?
发布于 2012-12-06 10:03:19
从您的RewriteRule中删除[L]标志,它们将自动合并。
https://stackoverflow.com/questions/13735230
复制相似问题