Search code examples
regex.htaccessmod-rewrite

Remove a part of the URL using REGEX and .htacess


I want to replace this URL:

mydomain.com/posts/1659-artigos/etc-to

By this one:

mydomain.com/etc-to

Using .htaccess I'm trying the following:

RewriteEngine on
RewriteRule ^posts/1659-artigos/(.*)$ $1

But it isn't working. No redirect happens.

Can anyone help?

Thanks!


Solution

  • Converting my comment to answer so that solution is easy to find for future visitors.

    You can use this code to get redirect working:

    RewriteEngine on
    
    RewriteRule ^posts/1659-artigos/(.*)$ /$1 [L,R=301,NC]
    

    You need to use / before $1 for external redirect and make sure to use R flag for full redirect.