KingStakh KingStakh - 3 months ago 11
Apache Configuration Question

How create rule in .htaccess to redirect

Have some problem after change CMS to Wordpress. My old url-s looks like http://example.com/news/sport/12123 where 12123 post ID. Now my url-s looks like http://example.com/12/12/2016/post-name. After migrate to WP I save in my DB old post ID's as custom field. Now I need redirect all user who comes to my site by old url's to new url's. For this I need in .htaccess add 301 redirect some like this:

Redirect 301 /news/sport/12123 /redirectold.php?oldid=12123


Where redirectold.php get from DB new url by old ID from url. If I open directly http://example.com/redirectold.php?oldid=12123 all works fine, but I don't know how redirect to /redirectold.php?oldid= all old links and get old post ID from old url and place it after /redirectold.php?oldid= in .htaccess.

E.g. if open old link http://example.com/news/sport/12123 need redirect it to http://example.com/redirectold.php?oldid=12123 via .htaccess

Answer

Try this,

RewriteEngine on
RewriteRule ^/news/sport/([0-9]+)$ /redirectold.php?oldid=$1 [R=301,L]