Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewrite old URLs using htaccess and regex

I am having an issue on a WordPress-based website I run regarding a Rewrite Rule in my .htaccess file.

Recently overhauled the website and my permalink structure has changed. Previously, it was

/%year%/%monthnum%/%postname%.html

Now, it is

/%year%/%monthnum%/%postname%/

The issue I'm running into is Google having all the .html links stored. So, visitors who follow the links via Google get a 404.

What I would like to happen is that anytime an incoming URL has the .html on the end and matches the permalink structure format, redirect to the same URL, just without the html extension.

I tried adding a Rewrite Rule to my .htaccess file and am having issues with it not working. I'm not sure what that issue is.

RewriteEngine On
RewriteBase /mywebsite.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4}+)/([0-9]{2}+)/([a-zA-Z0-9-]+).html$ $1/$2/$3/
like image 932
Matt. Avatar asked Nov 02 '22 08:11

Matt.


1 Answers

You're close but your regex is little bit off track. Try this rule:

RewriteEngine On
RewriteBase /mywebsite.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{2})/(.+?)\.html$ $1/$2/$3/ [L,NC,NE,R=301]
like image 86
anubhava Avatar answered Nov 09 '22 14:11

anubhava