2

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.

break
  • 43
  • 1
  • 3
  • Have you tried removing the `city/` part of the `RewriteRule` ? – Brewal Dec 16 '14 at 15:50
  • Maybe i didn't explain precise, its not problem to change url format to what i want and get it to work. Problem is when i do that, old urls will point to 404 error page, and i want to redirect users to proper new url page with 301, so i don't get problems with google and lose indexed pages. – break Dec 16 '14 at 15:56

3 Answers3

1

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]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Doesn't this require to change the existing RewriteRule by removing `city/` then ? – Brewal Dec 16 '14 at 16:00
  • This one works too :) But why this [L,NC,R=302] ? Can i put 301 instead 302 code? – break Dec 16 '14 at 16:26
  • Yes definitely put `301` after your testing. Also it is never a good idea to mix `RewriteRule` with `RedirectMatch` directive. That's the main reason I didn't suggest you a `RedirectMatch` based rule. – anubhava Dec 16 '14 at 16:28
  • So this approach is more friendly and is better practice? Glad to know. Thank you for your answer. – break Dec 16 '14 at 16:31
  • @JeremiahWinsley: Reason why it is not a good idea because they are processed in 2 different Apache modules `mod_alias` and `mod_rewrite`. And they are invoked at different stages. So if you have multiple rules mixed with these 2 directives then it is not easy to establish a sequence of rule execution. – anubhava Dec 16 '14 at 16:35
  • @JeremiahWinsley: I have probably read pretty much all the Apache manuals on rewrite so had read this one too. If you read between the lines you will understand the real intent of this article. btw none of other directives do a silent (internal) rewrite like the last rule in my answer above. – anubhava Dec 16 '14 at 17:04
0

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

Jeremiah Winsley
  • 2,537
  • 18
  • 30
-1

You can try this for the redirection:

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

Cms Coders
  • 41
  • 1
  • 6