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!

2 Answers 2

3

Try using this rule :

RewriteEngine on
RewriteRule ^dir/([^/]+)-([0-9]+)$ /dir/$2/$1 [L,R=301]
Sign up to request clarification or add additional context in comments.

Comments

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-]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.