SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Redirect old URLs and avoid 404s

    Hello,
    My old urls was like
    Code:
    http://site.com/news/183/Blue+Lagoon
    and this is htaccess
    Code:
    RewriteRule ^([a-z]+)(/([^/]{0,32})(/.+)?)?$  index.php?a=$1&q=$3    [L]
    Now im change url structure and my url is like
    Code:
    http://site.com/news/183
    and line in htaccess is
    Code:
    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

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try 301 redirect:
    Code:
    RewriteRule ^(.*)news/([0-9]+)/(.*?)$ news/$2 [R=301]
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much @MarPlo
    But this is what i need and its work perfect
    Code:
    RewriteRule ^(.*)news/([0-9]+)/(.*?)$ http://site.com/news/$2 [R=301]

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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?

    Code:
    RewriteEngine on
    RewriteRule ^news/([\d]+)/? news/$1 [R=301,L]
    Note the lack of end anchor in the regex.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  5. #5
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    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?

    Code:
    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
    Code:
    RewriteRule ^news/([\d]+)/? http://site.com/news/$1 [R=301,L]

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Member
    Join Date
    Jul 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    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
    Code:
    RewriteRule ^news/([\d]+)/? news/$1 [R=301,L]
    redirect page to
    Code:
    http://site.com/home/username/public_html/news/183
    And this works fine
    Code:
    RewriteRule ^news/([\d]+)/? http://site.com/news/$1 [R=301,L]

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    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
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •