大网数据广告活动
网站运营与故障问答

How do I do a catch-all .htaccess redirect?

· 52CE 我爱测

A site has moved from example.net to example.com/ie/en/ . (In other words, it’s moved from being its own domain to being a subfolder in a larger site.) However, the page structure has also changed. I have a few pages with specifi…

问题原文

原帖链接 · TRiG · CC BY-SA 3.0

A site has moved from example.net to example.com/ie/en/. (In other words, it’s moved from being its own domain to being a subfolder in a larger site.) However, the page structure has also changed. I have a few pages with specific redirects. For example, example.net/packaging should redirect to example.com/ie/en/packaging-solutions/. Everything else (the homepage and all pages not specifically mentioned) should redirect to example.com/ie/en/ directly.

Here’s what I tried:

Redirect permanent /geotextiles http://www.example.com/ie/en/technical-fabrics/geosynthetics/
Redirect permanent /fibcs http://www.example.com/ie/en/packaging-solutions/fibc-filling-solutions/
Redirect permanent /packaging http://www.example.com/ie/en/packaging-solutions/
Redirect permanent /bags-fibcs http://www.example.com/ie/en/packaging-solutions/fibc-filling-solutions/
Redirect permanent /horticultural http://www.example.com/ie/en/technical-fabrics/agri-horti-aquaculture/
Redirect permanent /construction http://www.example.com/ie/en/technical-fabrics/construction/
Redirect permanent / http://www.example.com/ie/en/

RewriteEngine On
RewriteRule ^(.*)$ http://www.example.com/ie/en/ [R=301]

This very nearly works. The pages mentioned specifically redirect as they should; so does the homepage. However, other pages do not work. example.net/blah should redirect to example.com/ie/en/, but instead redirects to example.net/ie/en/blah. Is there any simple way to lose the page name? The new website is completely restructured, and quite simply doesn’t have equivalent pages to some of those on the old site, so redirecting to the homepage makes sense.

Taking a tip from an answer, I tried this approach:

RewriteEngine On
RewriteRule /geotextiles http://www.example.com/ie/en/technical-fabrics/geosynthetics/ [R=301,L]
RewriteRule /fibcs http://www.example.com/ie/en/packaging-solutions/fibc-filling-solutions/ [R=301,L]
RewriteRule /packaging http://www.example.com/ie/en/packaging-solutions/ [R=301,L]
RewriteRule /bags-fibcs http://www.example.com/ie/en/packaging-solutions/fibc-filling-solutions/ [R=301,L]
RewriteRule /horticultural http://www.example.com/ie/en/technical-fabrics/agri-horti-aquaculture/ [R=301,L]
RewriteRule /construction http://www.example.com/ie/en/technical-fabrics/construction/ [R=301,L]
RewriteRule / http://www.example.com/ie/en/ [R=301,L]
RewriteRule ^(.*)$ http://www.example.com/ie/en/ [R=301,L]

And now everything redirects to www.example.com/ie/en/. All my special cases are being ignored, despite the L flag.

已采纳解答

原帖链接 · Tero Kilkanen · CC BY-SA 3.0

You should write all your rules using RewriteRule, since those two directives are handled by different modules, and things might not work as you expect if you use both modules.

For example:

Redirect permanent /geotextiles http://www.example.com/ie/en/technical-fabrics/geosynthetics/

is like this using RewriteRule:

RewriteRule ^geotextiles http://www.example.com/ie/en/technical-fabrics/geosynthetics/ [R=301,L]

Doing this most replacement could solve your problem.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2014-05-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。