2

This is the original I need to modify:

https://example.com/actualites-cyclisme/criterium-du-dauphine/5115-dauphine-libere-le-parcours-devoile-lalpe-dhuez-fait-son-retour

These are the things I have done (which is what I wanted!)

https://example.com/dauphine-libere-le-parcours-devoile-lalpe-dhuez-fait-son-retour/

But... I want to include a condition, for example I do not want theses URLs to be touched:

https://example.com/123456-dauphine-libere-le-parcours-devoile-lalpe-dhuez-fait-son-retour/

https://example.com/aaaaaa-123456-dauphine-libere-le-parcours-devoile-lalpe-dhuez-fait-son-retour/

Here's my Awesome regex :

RewriteRule /?(.*)([0-9]+)-(.*)$  /$3 [R=301,L]

I probably have to say that I NEED /(.*)/0000-post, but if I add a slash within the regex it does not work anymore.

I used http://htaccess.mwl.be btw.

  • If you make the slash mandatory (ie. remove the ?) then that rule should still match the first URL. And it should avoid the last two URLs from matching. If not then maybe something else is going on - please post your entire .htaccess file. Where is the trailing slash coming from? That's not part of your directive. (There's no need to have parenthesised sub groups in the regex when the backreferences are not needed.) – MrWhite Jul 22 '17 at 22:21
2

Could help some Wordpress user who want to not get 404 when changing permalinks.

# You dont want your medias to be rewritten
RewriteCond %{REQUEST_URI} !^/(wp-content/uploads)/

RewriteRule /?(.*)(/)([0-9]+)-(.*)$  /$4 [R=301,L]
  • Your directive/regex could be simplified... RewriteRule /\d+-(.*)$ /$ [R=301,L]`. (No need for captured groups (ie. parenthesised subpatterns) unless they are being used.) – MrWhite Dec 15 '17 at 11:31
  • 1
    You missed the 1 in the /$ -> /$1. You are right anyway, I am starting in the regex world and it is pretty hard :), thank you – Dinath Dec 17 '17 at 8:39
  • Ah yeah, oops! Looks like I hit the backtick (`) instead of the 1 by mistake (which also messed up the formatting)! It should be: RewriteRule /\d+-(.*)$ /$1 [R=301,L] (the \d is a shorthand character class, which is the same as [0-9]) – MrWhite Dec 17 '17 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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