2

I had a question about a problem I am having with a rewrite rule, I want to make a rewrite rule that gets everything after the hyphen, but it just gets the last word?

My Link:

http://www.website.com/home/24-nieuws/143-kip-kip-kip-donington.html

I have the following rule:

RewriteRule ^home/24-nieuws/(.*)-(.*).html$  http://www.website.com/$2 [R=301,L]

This rule gives me a weird output, well I find it weird, but It could because a hyphen is used as a regex character?

Output:

http://www.website.com/donington

It skips all of the text and just gets the last word? Does anyone know what I have to do to make it get everything between the first hyphen and the .html?

1 Answer 1

1

You can use this rule:

RewriteRule ^home/24-nieuws/[^-]*-(.+)\.html$  http://www.website.com/$1 [R=301,L,NC]

[^-]* is negated character class that match 0 or more non-hyphen characters. It will stop matching before first hyphen.

It is also possible to use a non-greedy quantifier to make it work:

RewriteRule ^home/24-nieuws/.*?-(.+)\.html$  http://www.website.com/$1 [R=301,L,NC]

RegEx Demo

Sign up to request clarification or add additional context in comments.

1 Comment

Oh thanks this works perfectly! I didn't know this, but now I do thanks very much! This will be a big help in the future :D

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.