3

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.

2 Answers 2

2

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.

6
  • "You should write all your rules using RewriteRule" - because you can't actually do what the OP is trying to do with mod_alias (Redirect). If you could then it would be perfectly acceptable to use mod_alias (for everything). But yes, like you say, you should not mix redirects in both mod_alias and mod_rewrite because the execution order is not clearly defined (+1).
    – MrWhite
    May 1, 2014 at 12:38
  • That's not working either. Have updated question with details.
    – TRiG
    May 1, 2014 at 12:43
  • @TRiG: The directory prefix (/) is actually removed for RewriteRule pattern matching, so /geotextiles should be ^geotextiles (to match at the start of the URL). You also need to remove the line that starts RewriteRule / http... since that is what the last directive is doing. You also don't need to capture the subpattern, so .* rather than ^(.*)$ is sufficient.
    – MrWhite
    May 1, 2014 at 13:16
  • That works, @w3d. Thanks. That means that this answer is very nearly right. If it's edited (or if w3d provides a new one), I'll accept.
    – TRiG
    May 1, 2014 at 13:32
  • 1
    You're welcome. I should just clarify... "The directory prefix (/) is removed for RewriteRule pattern matching" in per-directory .htaccess files. If this was to be used in httpd.conf it would still need the directory prefix.
    – MrWhite
    May 1, 2014 at 14:45
0

With RewriteRule, you should omit the / at the beginning of the url path. So in your case, the rules should be

RewriteRule geotextiles http://www.example.com/ie/en/technical-fabrics/geosynthetics/ [R=301,L]
1
  • The accepted answer already shows rewrite rules without the leading slash. How does your answer add anything? Nov 12, 2020 at 11:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.