Use mod_rewrite to remove year and month from wordpress post's url, while keeping year and month permalink structure
I've been around the world and back with this mod_rewrite syntax and I'm having a hard time constructing some working rewrite rules.
Scenario:
Working in Wordpress, I want to remove "year/month" from the url structure while keeping the year/month custom permalink activated. Reason being I'm using a plugin that generates a category specific archive, but the plugin is dependent on the permalink structure being:
/%cateogry%/%year%/%monthnum%/%postname%
Goal:
I want to change this path: category/2010/12/post-name
to this path: category/post-name
Condition:
If the year/month is present in the url and it is followed by a forward slash (/
) with characters after the slash (eg. category/2010/12/post-name
)
{
Remove the year/month from the url;
// intended output: category/post-name
// in this case I am directly clicking on a link.
} else {
//leave it alone;
//intended output: category/2010/12/
/*=======
In this case I am already in the archive section and will want the
plugin to display the archive according to the year & category
=======*/
}
I have this rule written in the .htaccess file
RewriteRule ^category/[0-9]{4}/[0-9]{2开发者_Python百科}/$ news/$1 [L, NC]
Can anyone help please (if you can, explain some of the things you are doing so I can familiarize myself more with it)?
Your question is a little confusing, but let me see if I understand.
You want to change:
http://somehost.com/category/2011/11/some-post
to:
<webroot>/category/some-post
If that's the case, you're almost there. In the rewrite you have a backreference ($1), but that backreference isn't defined anywhere in the search pattern. In order to define a backreference, you surround the portion with parenthesis. Updating your rule would look like this (also taking into account a variable category):
RewriteRule ^(.*)/[0-9]{4}/[0-9]{2}/(.*)$ $1/$2 [L,NC]
goto Admin - Settings - Permalinks
Select custom structure and use
/%postname%/
精彩评论