SE friendly URLs

Discussion in 'Programming' started by bscdesign.com, Feb 17, 2007.

  1. #1
    I am writing a blog script for my site and would like it to have SE friendly URLs. I have never done this before and don't know where to start. Is there some moderately simple way of doing this. I know that phpArcadeScript uses a .htaccess file in the main directory to do this. I need to do this in perl/cgi.
     
    bscdesign.com, Feb 17, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    designcode, Feb 18, 2007 IP
  3. bscdesign.com

    bscdesign.com Active Member

    Messages:
    681
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks I will check them out.
     
    bscdesign.com, Feb 18, 2007 IP
  4. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #4
    This is the code I use for my directory script. You may find this useful
    
    Options +FollowSymLinks
    # Turn on the redirect engine
    RewriteEngine on
    
    # Check whether the domain name begins with either www or localhost
    # else redirect to www.yourdomain.com
    # Remove the following 2 lines if you donot want that to happen
    RewriteCond %{HTTP_HOST} !^(www|localhost) [NC]
    RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NC,L]
    
    # yourdomain.com/path/to/dir/ will be rewritten to yourdomain.com/index.php?path=path/to/dir
    RewriteRule ^(.*)/$ index.php?path=$1 [QSA,L]
    # yourdomain.com/path/to/dir/page-{number}.html will be rewritten to yourdomain.com/index.php?path=path/to/dir&page={number}
    RewriteRule ^(.*)/([a-z0-9\-]+)([0-9]+).html$ index.php?page=$1&page=$3 [QSA,L]
    
    Code (markup):
     
    Aragorn, Feb 21, 2007 IP
  5. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I am usualy playing with $_SERVER["REDIRECT_URL"]

    I have a singel page site, then i redirect all 404 errors to a script error.php that decode what page they where trying to access with $_SERVER["REDIRECT_URL"], then i call a template file that display the page.
     
    ThomasNederman, Feb 21, 2007 IP
  6. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That's a good way of doing it, but don't forget to add a header('HTTP/1.1 200 OK'); on top of your script - otherwise Google and all other S.E. will think that those pages are missing (after all, Apache sends a 404 for them...)
     
    picouli, Feb 21, 2007 IP
  7. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #7
    But when the size and functions of the script increases, don't you think it is going to increase complexity? Also, I believe mod_rewrite will be much faster that that. What script is it?
     
    Aragorn, Feb 21, 2007 IP
  8. bscdesign.com

    bscdesign.com Active Member

    Messages:
    681
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Thanks everyone. I actually already got an answer on forum.modrewrite.com. But thanks for helping anyways.
     
    bscdesign.com, Feb 21, 2007 IP
  9. ColdMoney

    ColdMoney Peon

    Messages:
    65
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    ColdMoney, Feb 21, 2007 IP
  10. bscdesign.com

    bscdesign.com Active Member

    Messages:
    681
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Since people are still posting here I might as well ask some more questions here. And thanks ColdMoney but it isn't working right for me.



    This is the code I have so far:

    Options +FollowSymLinks
    
    RewriteEngine On
    
    RewriteRule ^(.*)\.html$ index.shtml?action=$1
    
    RewriteRule archives/(.*)-(.*)\.html$ index.shtml?action=showarchive&month=$1&year=$2
    
    RewriteRule topics/(.*)\.html$ index.shtml?action=showtopic&id=$1
    
    RewriteCond
    RewriteRule ^([^/]+)/([0-9]+)/([^/]+)\.html$ index.shtml?action=$1&id=$2&title=$3 [L]
    Code (markup):
    I want to add a condition that replaces any spaces or %20's in $3 with a dash (-). If that is possible. Otherwise I will have to edit the code in my .cgi files.

    Also when all is working will relative links work.
    Example:
    If the user is at mydomain.com/blog/posts/1/title-of-post.html and they click a link that links to ../../archives/2-2007.html will that bring them to mydomain.com/blog/archives/2-2007.html

    Thanks.
     
    bscdesign.com, Feb 23, 2007 IP
  11. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #11
    I have no idea on how to remove %20 using htaccess because, in my PHP script I have added the code to remove unwanted characters and make it SE friendly. I think that will be a better approach.
    The relative links will be working fine becoz the browser will be having no idea on whether you are using mod_rewrite or not. But I would suggest you to make the urls with respect to your homepage and set a Base Href as it will be much easier.
     
    Aragorn, Feb 24, 2007 IP
  12. bscdesign.com

    bscdesign.com Active Member

    Messages:
    681
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Thanks and just to double check..Is the code I showed above valid?
     
    bscdesign.com, Feb 24, 2007 IP