.htaccess regex url redirection

I am looking for a regex for a .htaccess file where I can redirect one URL to another.

URL1: / Dir / en-article-Title- 222

URL2: / DIR / 222 / en-article-header

I would like to redirect URL1 to 301 URL2.

In a high level language, I think I want to say .htaccess:

  • Take the numbers after the last dash (-)
  • Place these numbers in front of the article name with a trailing slash, i.e. 222 /
  • Remove last dash and numbers from article title

Any thoughts, suggestions, code are welcome!

+4
source share
2 answers

Try using this rule:

RewriteEngine on RewriteRule ^dir/([^/]+)-([0-9]+)$ /dir/$2/$1 [L,R=301] 
+3
source

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

Try this, keep in mind that an-article-title with this regular expression allows az, 0-9 and -, for more characters, edit the [\ w \ d-] part

0
source

Source: https://habr.com/ru/post/1432679/


All Articles