How to remove Post ID from posts and then redirect

2019-07-15 16:17发布

I just changed the permalink structure of my wordrpress based blog. Previously it had the permalink structure like this

http://www.mysite.com/sample-post-example/2345/

The numbers at the end of the url were automatically generated by wordpress software. In fact these numbers are post_id. Now I have this url structure.

http://www.mysite.com/sample-post-example/

Now the problem is that the individual posts are available under both url structures.

Is there anyway to redirect (301) old URLs to new url after removing the Post_ID present at the end of the old URL??

3条回答
戒情不戒烟
2楼-- · 2019-07-15 16:43

Individual posts should not be available under both URLs. Are you sure the second link you posted is not the archive page? This is probably the custom permalink structure you are looking for: /%category%/%postname%

查看更多
唯我独甜
3楼-- · 2019-07-15 16:45

I run a wordpress blog with the homepage containing the last 13 posts on frontpage. The homepage then has pagination to go to previous posts. So when I try to go to page number 2, which is like http://www.mysite.com/page/2/, I receive 404 errors due to above lines. Now I am using two lines and every thing is working fine on my blog. Please check them out and tell me is there anything wrong with these codes.

RedirectMatch 301 ^/([^/]+)/([0-9]{4})/$ http://www.mysite.com/$1
RedirectMatch 301 ^/([^/]+)/([0-9]{5})/$ http://www.mysite.com/$1

I have maximum ~1000 blog posts which make 74 pages. But the ID of the first post contains 4 digit number and the last post which I created with the ID at the end of the URL contains 5 digit number. So the first line check whether the post contains 4 digit number at the end of the URL, if yes it will remove them and display the remaining part which is /sample-post-example/ as described above. Similarly the second line detects the 5 digit number at the end of the url. The other URLs which contain less or more than 4-5 digit will remain unaffected. In this way the pagination of front page remains unaffected.

Can we combine the above two lines??

Sorry, eng is not my native language, so I think I couldn't tell you in a proper way.

查看更多
走好不送
4楼-- · 2019-07-15 16:48

Check this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule (.+)/\d{4,5}/?$ $1/ [R=301,L]
查看更多
登录 后发表回答