3

I have the following rewrite rule to rewrite my old to new URLs:

RewriteCond %{REQUEST_URI} !^/activity.*$ [NC]
RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L]

It must also ignore /members, not only /activity.

How can I achieve that?

  • 1
    Have you thought about just adding another RewriteCond directive? (^/activity.*$ is the same as simply ^/activity - the last .*$ is not required.) – MrWhite Apr 14 '15 at 7:51
  • tbh, I did not know that I can stack them... thanks! – Raphael Jeger Apr 14 '15 at 8:05
3

You can just add another RewriteCond directive:

RewriteCond %{REQUEST_URI} !^/activity
RewriteCond %{REQUEST_URI} !^/members
RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L]

Multiple RewriteCond directives are AND'd together by default. The OR flag can be used if required. I've removed the NC flag - unless you specifically need a case-insensitive match. As mentioned in comments, I've also simplified your regex slightly. .*$ says that the string can have any characters after the word "/activity", which is the same as a string that is simply prefixed with "/activity".

  • 1
    Oh, I somehow missed your explanation about the .*$, which was the reason for my post. +1 for you, I'm keeping mine for the OR option :) – Martijn Apr 14 '15 at 12:03
3

You can do this by adding another condition.

RewriteCond %{REQUEST_URI} !^/members [NC]
RewriteCond %{REQUEST_URI} !^/activity [NC]
RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L]

Or, sometimes simpeler, use the OR of a regex:

RewriteCond %{REQUEST_URI} !^/(members|activity) [NC]
RewriteRule ^(.*)/([0-9]+)/$ /$1/S$2/ [R=301,L]

I've removed the .*$ from your lines. That piece ment "any character (.*) untill the end ($) of the line. By removing that, you get the exact same effect, but more readable and I'm guessing it requires less resources because the regex is simpler.

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.