Sasidhar Boddeti
Sasidhar Boddeti

Reputation: 1214

Redirect already SEO friendly URL to another SEO friendly URL externally using .htaccess

Part of my .htaccess code is as follows:

RewriteRule ^([A-Z]+)*$ nextlevels_state.php?state=$1  [L]
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2  [NC]

This basically redirects URL, example.com/XX to example.com/nextlevels_state.php?state=XX and example.com/XX/1 to example.com/nextlevels_state.php?state=XX&page=1 (internally).

Now, I would like to change the URL structure of the URL from example.com/XX to example.com/nextlevels/XX and example.com/XX/1 to example.com/nextlevels/XX/1

and I tried changing .htaccess to as follows:

RewriteRule ^nextlevels/([A-Z]+)*$ nextlevels_state.php?state=$1  [L]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$ nextlevels_state.php?state=$1&page=$2  [NC]

However, as the site urls are already indexed in search engines, I would like to know a way to redirect all the traffic from example.com/XX to example.com/nextlevels/XX (externally) using .htaccess .

Please guide me in this regard. Thank you community :)

Upvotes: 1

Views: 221

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following, written and tested with shown samples only(improving your already done attempts here). Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON

##Rule for redirect to url example.com/nextlevels/XX.
RewriteRule ^([A-Z]+)$ nextlevels/$1 [R=301]
RewriteRule ^nextlevels/([A-Z]+)/?$ nextlevels_state.php?state=$1  [NC,L]

##Rule for redirect to url example.com/nextlevels/XX/1.
RewriteRule ^([A-Z]+)*/([0-9]+)$ nextlevels/$1/$2 [R=301]
RewriteRule ^nextlevels/([A-Z]+)*/([0-9]+)$  nextlevels_state.php?state=$1&page=$2  [NC,L]

Upvotes: 2

Related Questions