Reputation: 43
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.
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.
Upvotes: 2
Views: 2838
Reputation: 784958
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]
Upvotes: 1
Reputation: 2557
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
Upvotes: 0
Reputation: 41
You can try this for the redirection:
Redirect 301 /city/1_NewYork/ /1_NewYork/
Upvotes: -1