2

My current URL is

example.com/example/{dynamic_id}/{dynamic_name}/static/1.html

I want to redirect it to this,

example.com/{dynamic_name}/example/{dynamic_id}/static/1.html

I tried but failed.

2
  • Internal "rewrite" or external "redirect"? "My current url is" - Are you wanting to change your current URL structure? "i tried but failed." - what did you try? What format can the {dynamic_id} and {dynamic_name} take?
    – MrWhite
    Commented Oct 24, 2020 at 16:55
  • 1
    i want to redirect my old urls to new urls instead of 404. Commented Oct 25, 2020 at 6:54

1 Answer 1

3

Maybe something like:

RewriteEngine On

RewriteRule ^(example)/(\d+)/([\w-]+)/(static/1\.html)$ /$3/$1/$2/$4 [R=302,L]

This externally "redirects" a URL of the form /example/{dynamic_id}/{dynamic_name}/static/1.html to /{dynamic_name}/example/{dynamic_id}/static/1.html.

I'm assuming that {dynamic_id} consists of only digits and {dynamic_name} is alphanumeric and can include underscores (_) and hyphens (-).

$1, $2, $3 and $4 are backreferences to the corresponding captured groups in the RewriteRule pattern.

UPDATE: Specifically, the regular expression (regex) ^(example)/(\d+)/([\w-]+)/(static/1\.html)$ matches as follows:

  • ^ - asserts the start of the string. So whatever follows matches from the beginning of the URL-path.

  • (example) - matches the literal string "example" (at the start of the URL-path - note that the URL-path that is matched does not start with a slash). The surrounding parentheses ( ... ) make this a capturing group, which can later be referenced in the substitution string using $1 (the number indicates the position of the capturing group in the regex). Capturing this simply saves repetition later, as we can refer to this using $1, instead of having to repeat the word "example". This is a simple example but is good practice to save repetition of longer strings.

  • / - matches a literal slash.

  • (\d+) - matches 1 or more digits (ie. 0-9). \d is a shorthand character class that matches a digit 0-9. And the + quantifier matches 1 or more of these. Again, this is a capturing group, so is saved in the backreference $2.

  • ([\w-]+) - matches 1 or more word (or hyphen) characters. The \w is a shorthand character class indicating a word character, ie. a-z, A-Z, 0-9 or _. The hyphen (-) is added to the character class. This is another capturing group, so is saved in the backreference $3.

  • (static/1\.html) - matches the literal string "static/1.html". The dot (.) is a special meta-character that matches any character, so we need to backslash-escape the dot to match a literal dot. This is saved in the backreference $4.

  • $ - asserts the end of the string. So, the regex that occurs between the ^ (start) and $ (end) must match the entire URL-path.

can u solve this too, example.com/static/{dynamic_id}/{dynamic_name}.html to example.com/{dynamic_name}/static/{dynamic_id}.html

i tried this but did't work.
RewriteRule ^(example)/(\d+)/([\w-]+)$ /$3/$1/$2.html [R=302,L]

That is very close, but you are just missing the .html (specifically \.html in regex syntax) from the end of the last path segment, since you need to match {dynamic_name}.html, not just {dynamic_name}. (Without capturing the ".html" part.)

Consequently the above will fail to match and it just won't do anything (or it matches the wrong URL and redirects incorrectly).

So, you would need to change it slightly like this:

RewriteRule ^(static)/(\d+)/([\w-]+)\.html$ /$3/$1/$2.html [R=302,L]

The order of the two rules does not matter in this instance as there does not appear to be a conflict.

However, one thing to be aware of is redirect-loops. Where the URL you are redirecting "to" also matches a URL-pattern you are redirecting "from". But that looks OK here.

i want to redirect my old urls to new urls instead of 404

Note that the above are currently 302 (temporary) redirects. However, if you are redirectign "old" URLs then you probably want to change these to 301 (permanent) at some point. Always good to test with 302s to avoid potential caching issues.

4
  • can u solve this too, example.com/static/{dynamic_id}/{dynamic_name}.html to example.com/{dynamic_name}/static/{dynamic_id}.html which is very similar but with just 3 path segments instead of 4. @MrWhite Commented Oct 25, 2020 at 7:33
  • That is left as an exercise for the reader ;) - it's exactly the same principle as above. Have a go, if you get stuck, post your attempt and I'll have a look.
    – MrWhite
    Commented Oct 25, 2020 at 12:01
  • 1
    i tried this but didn't work. RewriteRule ^(example)/(\d+)/([\w-]+)$ /$3/$1/$2.html [R=302,L] Commented Oct 25, 2020 at 15:53
  • Looks like you are just missing the .html part from the end of the last path segment, so the above will fail to match. I've updated my answer with more explanation about the regex and addressed this secondary URL redirect.
    – MrWhite
    Commented Oct 25, 2020 at 19:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.