Search code examples
apache.htaccesshttp-redirect

Redirection with .htaccess for many pages by Regex?


I would like a redirection for many pages (10 000). For example, I need :

example.com/home-office-london/ to
example.com/office-london/

and

example.com/home-office-paris/ to
example.com/office-paris/

I want this with a 301 redirection by .htaccess.

Can you help me please?


Solution

  • From your examples it would seem you are removing home- from the start of the URL-path. These URLs consist of a single path segment and end with a slash.

    You can do this using mod_rewrite (since I assume you are already using mod_rewrite) at the top of the root .htaccess file:

    RewriteEngine On
    
    # Redirect "/home-<anything>/" to "/<anything>/"
    RewriteRule ^home-([^/]+/)$ /$1 [R=301,L]
    

    Test first with a 302 (temporary) redirect to avoid potential caching issues.