How can I redirect specific parts of a URL to a new URL using htaccess?

In building a new site I need to restructure some of the links, and I need to redirect the old links to the new links. I have only a few, and are as follows:

about|what-we-do => about-us
contact => get-in-touch
our-work|projects => featured-work

I want to be able to keep anything they put after those links, also. For example, I'd like the redirects to be like this:

our-work/web/project-title => featured-work/web/project-title

This is what I have in my htaccess file at this point, which doesn't work.

RewriteRule ^(about|what-we-do)(/)?  /about-us/$1      [QSA,NC,L,R=301]
RewriteRule ^contact(/)?             /get-in-touch/$1  [QSA,NC,L,R=301]
RewriteRule ^(our-work|projects)(/)? /featured-work/$1 [QSA,NC,L,R=301]

RewriteRule ^about-us(/)?$           /pg.about.php     [QSA,NC,L]
RewriteRule ^get-in-touch(/)?$       /pg.contact.php   [QSA,NC,L]
RewriteRule ^featured-work(/)?$      /pg.projects.php  [QSA,NC,L]
RewriteRule ^hello(/)?$              /pg.hello.php     [QSA,NC,L]

If I try and visit something like /about then I'm redirected to /about-us/about/. I'm not well-versed in htaccess. So, if anyone is able to help me, it would be much appreciated. I'll keep plugging away at it. Thanks in advance!

Upvotes: 1

Views: 75

Answers (1)

anubhava
anubhava

Reputation: 785256

Your rules are not capturing URI part after first slash. You can use:

RewriteEngine On

RewriteRule ^(?:about|what-we-do)(/.*)?$  /about-us$1 [NC,L,R=301]
RewriteRule ^contact(/.*)?$               /get-in-touch$1 [NC,L,R=301]
RewriteRule ^(?:our-work|projects)(/.*)?$ /featured-work$1 [NC,L,R=301]

RewriteRule ^about-us/?$           /pg.about.php     [NC,L]
RewriteRule ^get-in-touch/?$       /pg.contact.php   [NC,L]
RewriteRule ^featured-work/?$      /pg.projects.php  [NC,L]
RewriteRule ^hello/?$              /pg.hello.php     [NC,L]

Upvotes: 2

Related Questions