casusbelli
casusbelli

Reputation: 463

Redirect rewritten URL to a new rewritten URL

I'm working on my website link architecture. I would like to redirect with my .htaccess already rewritten URL to a new one to keep my incoming links active.

www.website.com/profile-info/ to www.website.com/profile/

This is my actual working .htaccess :

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_URI} /profile-info/?$ [NC]
RewriteRule . models.php [L]

So in order to redirect /profile-info/ to the new URL : /profile/ . I end up with this code. However it redirects /profile/ to /profile-info/.

RewriteCond %{REQUEST_URI} /profile/?$ [NC]
RewriteRule . /profile-info/ [L,R=302]

Upvotes: 0

Views: 131

Answers (2)

arkascha
arkascha

Reputation: 42885

I think this is what you are looking for:

RewriteEngine On
RewriteMap /
RewriteRule ^/?profile-info/?$ /profile [END,NE,R=301,QSA]
RewriteRule ^/?profile/?$ models.php [L,QSA]

It is a two step strategy:

  1. redirect "old" links pointing to /profile-info to the new /profile
  2. internally rewrite /profile to models.php

You may have to change the RewriteMap, this obviously depends on your situation.


And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often without reason. They are only provided for situation where you do not have access to the host configuration (read: really cheap hosting providers) or in case an application needs to write its own rules (which is an obvious security nightmare...).

Upvotes: 1

anubhava
anubhava

Reputation: 784908

You can use these 2 rules:

Options +FollowSymlinks -MultiViews
RewriteEngine On

# redirect /profile-info to /profile
RewriteRule ^profile-info/?$ /profile [R=301,NE,L]

# rewrite /profile to /models.php
RewriteRule ^/?profile/?$ models.php [L,NC]

Upvotes: 1

Related Questions