Gavin Mannion
Gavin Mannion

Reputation: 905

Change .htaccess to 301 Redirect and REMOVE wildcard category from URL

I've got a large wordpress site that has been using the permalink structure of http://[root]/[category]/[postname] for the last 6 years and now I'd like to change it to remove the category part of the URL.

I can do this easily but obviously I will then have a problem with Search Engines sending people to the incorrect address so I want to add a line in my .htaccess to 301 redirect people to the new path without the category.

However the category is dynamic AND it mustn't changed the URL if the URL has the following terms in it

(image, images, wp-content, wp-admin, wp-includes)

I'd like the list to be in an array so I can add to it if I find other names that mustn't change.

So as an example

http://www.lazygamer.net/sony/sonys-trying-trademark-lets-plays/

should become

http://www.lazygamer.net/sonys-trying-trademark-lets-plays/

but

http://www.lazygamer.net/images/2014/07/Lets-play-a-game_thumb.jpg

must not change

Upvotes: 0

Views: 499

Answers (1)

Praemon
Praemon

Reputation: 851

If those urls including "image", "images", "wp-content" etc. are actual files or directories on the server, you can exclude them form the rewrite rule for the dynamic content:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)/([^\/]+)/?$ /$2/ [r=301,nc,L]

Upvotes: 1

Related Questions