htaccess redirect with Regex. How to remove .shtml and add forward slash?

JasonSc

BuSo Pro
Joined
Mar 30, 2016
Messages
109
Likes
144
Degree
1
htaccess redirect with regex

Long story but I'm "inherited" a legacy site which is being migrated over to wordpress.

Currently most of the pages end with a ".shtml". I have kept the slugs the same, but they end with the "/" not ".shtml"

Using Regex, how do a I create a 301 redirect rule to have the ".shtml" to redirect to the "/"

example:
old url: abc.com/phone.shtml
new url: abc.com/phone/

There are a little over 200 pages and I don't want to create a line by 301 redirect.

Oh and one more. I want to change the blog slug. Currently its blog/year/date/title.shtml
old: abc.com/blog/2016/10/title.shtml
new: abc.com/blog/title/
 
Last edited:
htaccess redirect with regex

Long story but I'm "inherited" a legacy site which is being migrated over to wordpress.

Currently most of the pages end with a ".shtml". I have kept the slugs the same, but they end with the "/" not ".shtml"

Using Regex, how do a I create a 301 redirect rule to have the ".shtml" to redirect to the "/"

example:
old url: abc.com/phone.shtml
new url: abc.com/phone/

There are a little over 200 pages and I don't want to create a line by 301 redirect.

Oh and one more. I want to change the blog slug. Currently its blog/year/date/title.shtml
old: abc.com/blog/2016/10/title.shtml
new: abc.com/blog/title/

Assuming Apache since it's Wordpress related.

Beware this is untested and may need slight modification to work.


Code:
.htaccess

RewriteEngine on
RewriteRule ^/(.*)\.shtml$ /$1 [NC]
RewriteRule ^/blog/(\d+)/(\d+)/(.*)\.shtml$ /blog/$1/$2/$3 [NC, L]
 
Assuming Apache since it's Wordpress related.

Beware this is untested and may need slight modification to work.


Code:
.htaccess

RewriteEngine on
RewriteRule ^/(.*)\.shtml$ /$1 [NC]
RewriteRule ^/blog/(\d+)/(\d+)/(.*)\.shtml$ /blog/$1/$2/$3 [NC, L]

Thanks for the help. The above code didn't work, but it got me pointed in the right direction.

I got it fixed sort of... The code below works, but its does create a double hope 301. Which isn't ideal, but will work for the time being.

Code:
RedirectMatch 301 (.*).shtml$ https://www.domain.com$1/
RedirectMatch 301 ^/blog/([0-9]{4})/([0-9]{2})/(?!page/)(.+)$ /blog/$3
 
Last edited:
If you want to backend manage your 301s you could use a plugin like: https://wordpress.org/plugins/redirection/ there are many others, simple to use and at quick glance easier to manage. (For me anyway)

I thought about going that direction, but I just don’t like plugins. Having had several sites hacked in the past I try to keep plugins to the bare minimum.
 
Back