I am using the following permalink structure:
/%postname%-%monthnum%%year%%post_id%.html
I want to get it changed to just /%postname%/
.
Please help me with what should be the correct redirect rule for .htaccess
.
Assuming you have changed your permalinks in WordPress (under Settings > Permalinks), as mentioned earlier, then you can redirect your old URLs en masse (in order to preserve SEO) with something like the following at the top of your .htaccess
file, using mod_rewrite:
RewriteRule ^([\w-]+)-\d{7,}\.html$ /$1/ [R=302,L]
This will redirect URLs of the form /%postname%-%monthnum%%year%%post_id%.html
to /%postname%/
, assuming your post name is limited to the characters A-Z
, a-z
, 0-9
, _
, -
.
As noted above, this must go before your WordPress front-controller (ie. before the # BEGIN WordPress
section), at the top of your .htaccess
file.
Note also, that this is currently a temporary (302) redirect. Change the 302
to 301
(permanent) only once you have tested that this is working OK. This is to avoid any problems with browser caching.
So basically you want to change the URL structure and then redirect the old URLs to the new ones?
As noted by MrWhite, you change the premalink structure in WordPress, under Settings->Permalinks.
Once you have changed them, you can redirect the old URLs one by one by adding this command and using your real URLs:
RedirectPermanent /old-file.html http://www.domain.com/new-file
You can either do this in .htaccess or in your control panel on the server, if you have such.
Redirect
and mod_rewrite in a single config file - since the mod_rewrite directives will execute first, regardless of the apparent order of directives in the file (which can result in some unexpected conflicts).
.htaccess
. (?)