4

I'm looking for a regular expression for a .htaccess file where I can redirect one URL to another.

URL1: /dir/an-article-title-222

URL2: /dir/222/an-article-title

I would like URL1 to 301 redirect to URL2.

In highlevel language, I think I want to tell .htaccess to:

  1. Take the digits after the last dash (-)
  2. Put those digits before the article name with a trailing slash, i.e. 222/
  3. Remove the last dash and digits from the article name

Any thoughts, suggestions, code are welcome!

Shree
  • 77
  • 1
  • 6

2 Answers2

3

Try using this rule :

RewriteEngine on
RewriteRule ^dir/([^/]+)-([0-9]+)$ /dir/$2/$1 [L,R=301]
Oussama Jilal
  • 7,669
  • 2
  • 30
  • 53
0

RewriteRule ^/dir/([\w\d-]*)-([\d]+)$/ /dir/$2/$1 [L,R=301]

Try this, keep in mind that an-article-title by this regex allow a-z, 0-9 and -, for more chars edit the the part [\w\d-]

donald123
  • 5,638
  • 3
  • 26
  • 23