Home > Software design >  Redirection with .htaccess for many pages by Regex?
Redirection with .htaccess for many pages by Regex?

Time:06-10

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?

CodePudding user response:

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.

CodePudding user response:

That's ok for me. Thanks a lot.

RewriteRule ^home-office-([^/] /)$ /office-$1 [R=301,L]
  • Related