skip to Main Content

The following .htaccess rule replaces "OLDNAME" with "NEWNAME" in a URL and it removes the string "-2" from the end of the URL if it exists. I need to update the rule to accommodate two scenarios: "OLDNAME1" and "OLDNAME2". Both redirect to a single "NEWNAME".

RewriteRule ^(index.php/)?OLDNAME/(.+?)(?:-2)?/?$ /NEWNAME/$2 [L,NC,R=302,NE]

I’ve tried several variations of the following:

RewriteRule ^(index.php/)?(OLDNAME1|OLDNAME2)/(.+?)(?:-2)?/?$ /NEWNAME/$2 [L,NC,R=302,NE]

Any help appreciated.

2

Answers


  1. With your shown samples please try following .htaccess rules file. We need to use THE_REQUEST variable here and also make sure to clear your browser cache before testing your URLs.

    RewriteEngine ON
    RewriteCond %{THE_REQUEST} s/?index.php/??(?:OLDNAME[12])(.+?)(?:-2)s [NC]
    RewriteRule ^ /NEWNAME/%1? [R=301,L]
    
    Login or Signup to reply.
  2. You are pretty close, you can do like this:

    # /nwblog => /blog
    RewriteRule ^(?:index.php/)?nwblog/(.+?)(?:-2)?/?$ /blog/$1 [L,NC,R=302,NE]
    
    # remove /index.php/
    RewriteRule ^index.php/(.+)$ /$1 [L,NC,R=302,NE]
    
    # remove /-2
    RewriteRule ^(.+)-2/?$ /$1 [L,NC,R=302,NE]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search