Search code examples
php.htaccesscodeigniterhttp-redirectexpressionengine

redirect and change a single URL segment


We have several URLS and want to re-direct based on one of the segments.

Usually we've have the rewrite rule then something like:

redirect permanent /home/category/feline /home/category/cat

The challenge is there's something like 50+ urls and the segment changes, so I wanted to know if there is a simpler way to match and redirect?

Here's a few examples:

redirect permanent /home/category/feline /home/category/cat
redirect permanent /content/category/feline /home/category/cat
redirect permanent /channel/segment/category/feline /channel/segment/category/cat
redirect permanent /segment/category/feline/entry /segment/category/cat/entry

Is is possible to redirect any URLs with feline to cat?

I'm currently testing the following, which works, but wanted to clarify:

RewriteRule (.*)feline(.*)$ $1cat$2 [R=301,NE,L]

Solution

  • You can use RedirectMatch with regex support:

    RedirectMatch 301 ^(.*/)?feline(/.*)?$ $1cat$2
    

    Clear your browser cache before testing this rule.

    Using mod_rewrite:

    RewriteEngine On
    RewriteRule ^(.*/)?feline(/.*)?$ /$1cat$2 [R=301,NE,NC,L]