SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Is Still Alive silver trophybronze trophy RetroNetro's Avatar
    Join Date
    Jul 2003
    Location
    Texas
    Posts
    6,883
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    301 redirect troubles

    I have a site that I just redid and I want to 301 redirect all old URL's to the new ones.

    The old site was: /keyword_keyword_keyword.html the new site is /keyword-keyword-keyword/

    So I'm trying to replace underscores with dashes and then replace the .html with a forward slash.

    I have this:

    Code:
    DirectoryIndex index.php index.html
    
    # BEGIN WordPress
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
    
    # BEGIN 301 Redirects
    
    # REPLACE one underscore at a time until there is one or less left
    RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
    
    # REPLACE the last underscrore and do an external redirect
    RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
    
    # Replace .html with a /
    RedirectMatch 301 (.*)\.html$ http://http://www.website.com/$1/
    
    # END 301 Redirects
    It does not seem to be working as old URL's redirect to the front page. Any advice is appreciated.

  2. #2
    SitePoint Member
    Join Date
    Oct 2008
    Location
    Chennai
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by RetroNetro View Post
    I have a site that I just redid and I want to 301 redirect all old URL's to the new ones.

    The old site was: /keyword_keyword_keyword.html the new site is /keyword-keyword-keyword/

    So I'm trying to replace underscores with dashes and then replace the .html with a forward slash.

    I have this:

    Code:
    DirectoryIndex index.php index.html
    
    # BEGIN WordPress
    
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
    
    # BEGIN 301 Redirects
    
    # REPLACE one underscore at a time until there is one or less left
    RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
    
    # REPLACE the last underscrore and do an external redirect
    RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
    
    # Replace .html with a /
    RedirectMatch 301 (.*)\.html$ http://http://www.website.com/$1/
    
    # END 301 Redirects
    It does not seem to be working as old URL's redirect to the front page. Any advice is appreciated.
    Hi,

    You should use this code i think it will help for you

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_URI} \.html$
    RewriteRule ^(.*)\.html$ $1 [R=301,L]

    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hmmm, someone merged these two into the same thread. Since they have the same problem, no worries, though!

    Gentlemen, your problem is in the ordering of your mod_rewrite statements because the WP code you're using will direct before converting the _'s to -'s (personally, I'd keep the _'s and convert before taking the code to the query phase but that's not in your question).
    Code:
    DirectoryIndex index.php index.html
    
    # BEGIN 301 Redirects - Redirect implies the mod_alias Redirect directive
    
    # REPLACE one underscore at a time until there is one or less left
    # You're attempting to do two at a time
    RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
    
    # REPLACE the last underscrore and do an external redirect
    RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
    
    # Replace .html with a /
    RedirectMatch 301 (.*)\.html$ http://http://www.website.com/$1/
    
    # Rather than repeat code already provided
    # in my signature's tutorial article, I'll let you
    # go there to fetch the character-by-character
    # replacement code from there
    # - but it goes BEFORE the WP code!
    
    # Otherwise, if you can guarantee that there will ALWAYS be exactly three _'s
    RewriteRule ^([^_]+)_([^_]+)_([^_]+)$ $1-$2-$3 [R=301,L]
    
    # END 301 Redirects
    
    # BEGIN WordPress
    
    RewriteEngine On
    RewriteBase / # where's the Redirect that this undoes? DELETE!
    RewriteRule ^index\.php$ - [L] # already handled by the !-f
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    # END WordPress
    Despite the errors in your code, I must commend you for removing the WP <IfModule> wrappers which are HIGHLY ABUSIVE of the server!

    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

  4. #4
    Is Still Alive silver trophybronze trophy RetroNetro's Avatar
    Join Date
    Jul 2003
    Location
    Texas
    Posts
    6,883
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I don't know anything about this sort of stuff, but one thing I have learned (from you) is to remove the <IfModule> stuff. :P

    Thanks for your reply David.

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Hi Ben!

    Well, you've learned that much - go for the gusto and learn how to use mod_rewrite!

    Okay, I'll leave that alone. At least I know that my rants helped one member!

    Did the above post help you with your problem?

    [aside]DFW? I'd lived in Grapevine for a while (at Braniff's zenith many years ago). As the guy in the third seat, I'd hear "and they pay us to do this" and can only wish for another chance at life 'cause "I know what I want to be when I grow up."[/aside]


    Oh, sorry not to have read your "formerly" (if it was there last night) - good to see you again.

    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

  6. #6
    SitePoint Member
    Join Date
    May 2011
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am facing the same problem again...any one please help me?

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,656
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    vait,

    Put your code BEFORE the WP code. In general, order from most specific rule set to the most generic. Because the WP code will redirect EVERYTHING, it should be last (after you make your _ to - changes).

    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
  •