Using regex in htaccess for 301 redirects

I have a Wordpress blog in which I redirect blog posts from my old blog to my new blog in the following format:

The old blog is called "News", and the new blog is simply called "Blog" - both exist in the same domain in a subdirectory, as described below.

OLD News Blog Structure

http://www.example.com/news/new-android-os-3431 

The structure of the blog NEW Blog.

 http://www.example.com/blog/new-android-os 

Essentially, this redirect should do 2 things: -

  • Redirecting to the Blog Directory
  • Keep the message name in the same structure, but delete the last number set at the end of the URL.

I have about 900+ messages for which I need to set up redirects - I know that I can manually add them, but it will take some time. Can someone point out if this can use the regex directly in the htaccess file to minimize this process?

Now my htaccess looks like this:

 # BEGIN WordPress <IfModule mod_rewrite.c> RewriteEngine On RewriteBase /news/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /news/index.php [L] </IfModule> # END WordPress 
+7
source share
1 answer

Using mod_alias:

 RedirectMatch 301 ^/news/(.+?)(-[0-9]+)?$ /blog/$1 

or using mod_rewrite:

 RewriteEngine On RewriteRule ^news/(.+?)(-[0-9]+)?$ /blog/$1 [L,R=301] 
+14
source

All Articles