Old dynamic URLs to new SEO URLs

Discussion in 'Apache' started by webmasterplace, Jul 2, 2011.

  1. #1
    I'm currently rewriting my website to get some SEO URLs.

    It was working pretty good, but now I'm stuck at my final 301 redirects.
    I'd like to redirect the old dynamic URLs to the new static URLs (to prevent duplicate content).

    Examples:

    forum/viewforum.php?forum_id=123 ==> redirect to forum/forum-123/
    forum/forum-123 ==> redirect to forum/forum-123/ (must have trailing slash)
    forum/forum-123/ ==> stays like this, but apache handles this as forum/viewforum.php?forum_id=123

    This all works, but the pages don't get displayed.
    I always get:

    These are my rewriting rules:

    # Force trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://www.mysite.com/$1/ [L,R=301]
    
    # Forum SEO URL
    RewriteRule ^forum/forum-([0-9]+)/$ forum/viewforum.php?forum_id=$1 [L]
    
    # Redirect old dynamic URL
    RewriteCond %{QUERY_STRING} forum_id=([0-9]+)
    RewriteRule ^forum/viewforum.php$ http://www.mysite.com/forum/forum-%1/? [R=301,L]
    Code (markup):
    If anyone could point me into the right direction :)

    Thanks!
     
    webmasterplace, Jul 2, 2011 IP
  2. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try taking out the domain:

    
    # Redirect old dynamic URL
    RewriteCond %{QUERY_STRING} forum_id=([0-9]+)
    RewriteRule ^forum/viewforum.php$ forum/forum-%1/? [R=301,L]
    
    Code (markup):
     
    MartinPrestovic, Jul 6, 2011 IP
  3. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #3
    webmasterplace, Jul 7, 2011 IP
  4. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Where exactly are you working and what is your set up? The htaccess file you originally posted contains domains so that would indicate that your working on a remote server, but the error you just posted indicates your working locally in a xampp set up.
     
    MartinPrestovic, Jul 7, 2011 IP
  5. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well actually it's like this (testing locally):

    # Force trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://localhost/testing/$1/ [L,R=301]
    
    # Forum SEO URL
    RewriteRule ^forum/forum-([0-9]+)/$ forum/viewforum.php?forum_id=$1 [L]
    
    # Redirect old dynamic URL
    RewriteCond %{QUERY_STRING} forum_id=([0-9]+)
    RewriteRule ^forum/viewforum.php$ http://localhost/testing/forum/forum-%1/? [R=301,L]
    Code (markup):
     
    webmasterplace, Jul 7, 2011 IP
  6. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Ok, so lets just work with the local and then once you have it working you can add in the domains later.

    One thing that is confusing me is the "testing" directory, you have it in the first rule, not at all in the second rule and then half in the third rule.

    Give this version a try and see where it gets you:

    
    # Force trailing slash
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !index.php
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ^(.*)$ http://localhost/testing/$1/ [L,R=301]
    
    # Forum SEO URL
    RewriteRule ^testing/forum/forum-([0-9]+)/$ /testing/forum/viewforum.php?forum_id=$1 [L]
    
    # Redirect old dynamic URL
    RewriteCond %{QUERY_STRING} forum_id=([0-9]+)
    RewriteRule ^testing/forum/viewforum.php$ /testing/forum/forum-%1/? [R=301,L]
    
    Code (markup):
     
    MartinPrestovic, Jul 7, 2011 IP
  7. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Now I get a 404 :(
     
    webmasterplace, Jul 9, 2011 IP
  8. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    What do the error log files tell you? They can be a great help in determining whats wrong with the htaccess.
     
    MartinPrestovic, Jul 11, 2011 IP
  9. webmasterplace

    webmasterplace Peon

    Messages:
    802
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    0
    #9
    There's only unuseful information in the error log:

     
    webmasterplace, Jul 14, 2011 IP
  10. MartinPrestovic

    MartinPrestovic Peon

    Messages:
    213
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    That tells us that it isn't running the second rule. Try changing [R=301,L] to [R=301] on the third rule and see if that gives the same errors.
     
    MartinPrestovic, Jul 14, 2011 IP