Redirect old URLs and avoid 404s

Hello,
My old urls was like

http://site.com/news/183/Blue+Lagoon

and this is htaccess

RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$  index.php?a=$1&q=$3    [L]

Now im change url structure and my url is like

http://site.com/news/183

and line in htaccess is

RewriteRule ^([a-z]+)(/([^/]{0,32})?)?$  index.php?a=$1&q=$3    [L,NC] 

Now i have 404 error when users come from google on my site , also google webmaster tools report me about this error, how i can redirect old url to new one with htaccess…
Sp i need just redirect htxp://site.com/news/183/Blue+Lagoon to htxp://site.com/news/183

Hi,
Try 301 redirect:

RewriteRule ^(.*)news/([0-9]+)/(.*?)$ news/$2 [R=301]

Thank you very much @MarPlo
But this is what i need and its work perfect

RewriteRule ^(.*)news/([0-9]+)/(.*?)$ http://site.com/news/$2 [R=301]

AmN,

WHY would you use (.*) before news? WHY would you specify / followed by nothing or anything after the digits when all you need is the set of digits to be matched?

RewriteEngine on
RewriteRule ^news/([\\d]+)/? news/$1 [R=301,L]

Note the lack of end anchor in the regex.

Regards,

DK

Yes, you’re right, but also must be like this

RewriteRule ^news/([\\d]+)/? http://site.com/news/$1 [R=301,L]

AmN,

How is that any differrent? My redirection from the DocumentRoot is directly into your news directory as you have it, too. The only thing that your external redirection does is to remove a possible www. subdomain which was not part of your specification. If it is (suddenly), it needs to be handled separately and, preferably, before this redirection.

You might benefit from reading the mod_rewrite tutorial linked in my signature as it contains explanations and sample code. It’s helped may members and should help you, too.

Regards,

DK

This

RewriteRule ^news/([\\d]+)/? news/$1 [R=301,L]

redirect page to

http://site.com[B]/home/username/public_html/news/183[/B]

And this works fine

RewriteRule ^news/([\\d]+)/? http://site.com/news/$1 [R=301,L]

AmN,

Adding the server’s path/to/file is a problem with the server which can usually be corrected with a server restart.

Regards,

DK