Home > Software engineering >  How do I make this regex rewrite rule work for both with / and without /?
How do I make this regex rewrite rule work for both with / and without /?

Time:06-21

I made the following redirect rule to be used in htaccess for Wordpress which seems to work:

RewriteCond %{REQUEST_URI} /nl/my-impact/
RewriteRule ^nl/(. )$ $1nl/ [R=301,L]

But it should redirect both:

  • https://alivetoearth.org/nl/my-impact/florian
  • https://alivetoearth.org/nl/my-impact/florian/

to: https://alivetoearth.org/my-impact/florian/nl/

How can I make sure it works in both cases?

Now I can just make it work in one or the other. If there is a better way to write the entire redirect I would also love to know.

CodePudding user response:

You may use this redirect rule:

RewriteRule ^(nl)/([^/] /[^/] )/?$ /$2/$1/ [R=301,L,NC]
  • Related