redirect - Redirecting old permalink structure to new in htaccess

admin2025-06-05  3

I recently changed the permalink structure on my 2-month-old blog to a user friendly one which means a whole bunch of 404s. I wish to redirect the following:

.html

to

/

Before coming here, I spent days looking for this redirect online but never found. The redirect plugins recommended don't work. I just need a .htaccess rule to fix this.

I recently changed the permalink structure on my 2-month-old blog to a user friendly one which means a whole bunch of 404s. I wish to redirect the following:

https://example/poastname.html

to

https://example/postname/

Before coming here, I spent days looking for this redirect online but never found. The redirect plugins recommended don't work. I just need a .htaccess rule to fix this.

Share Improve this question edited Dec 15, 2018 at 2:23 MrWhite 3,8911 gold badge20 silver badges23 bronze badges asked Dec 14, 2018 at 15:53 Eze King EkeEze King Eke 112 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

Try adding this code to .htaccess:

RedirectMatch 404 /\d{4}/\d{2}/\d{2}/(.*)\.html http://example/$1

It should fix the issue.

You would need to do something like the following at the top of your .htaccess file, before the existing front-controller (ie. before # BEGIN WordPress).

RewriteRule ^([\w-]+)\.html$ /$1/ [R=302,L]

This assumes your "postname" consists of just the characters a-z, A-Z, 0-9, _ and -.

The RewriteRule pattern captures the postname before the .html URL suffix and saves this in the $1 backreference, which is used in the substitution string (2nd argument).

Note that this is a 302 (temporary) redirect. Only change it to 301 (permanent) when you are sure it's work OK, in order to avoid any potential caching issues. As always, clear your browser cache before testing.

You should use mod_rewrite (as opposed to mod_alias Redirect or RedirectMatch) in order to avoid conflicts with existing mod_rewrite directives. mod_rewrite executes before mod_alias, despite the apparent order of the directives in the config file.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749093066a316304.html

最新回复(0)