301 dynamic url redirect to new dynamic urls

2.7k views Asked by At

first of all, i try to find solution already in topics but didn't find something close to my problem so i decide to ask here. I'm beginner when comes to htacces and dynamic redirects, so i hope i will learn more about that from your help.

I need to redirect my old dynamic urls to new url format with 301 redirect in htaccess.

I have db with city list in format:

http://www.exampledomain.com/city/1_NewYork/

I want to change my urls to new format like example below:

http://www.exampledomain.com/1_NewYork/

I have more than 300 cities in database and i want to redirect old urls to new format with 301 redirect automatically , so i dont have to type manual 301 redirects in htaccess for every city.

Current Htaccess:

RewriteEngine On

RewriteRule ^city/([-]?[0-9]+)([-_][^/]*)? index.php?view=main&cityid=$1 [QSA]


Sorry for my English, i know its not the best :)

Thanks in advance.

3

There are 3 answers

6
anubhava On BEST ANSWER

You can use this rule as your very first rule before other rules:

RewriteEngine On

RewriteRule ^city/([^/]+)/?$ /$1 [L,NC,R=302]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?view=main&cityid=$1 [L,QSA]
1
Cms Coders On

You can try this for the redirection:

Redirect 301 /city/1_NewYork/ /1_NewYork/

0
Jeremiah Winsley On

You can use RedirectMatch for this.

RedirectMatch 301 /city/(.*) /$1

and replace your rewrite rule with

RewriteRule ^([-]?[0-9]+)([-_][^/]*)? index.php?view=main&cityid=$1 [QSA]

See http://httpd.apache.org/docs/2.2/mod/mod_alias.html#redirectmatch