1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Mod Rewrite Beginners Query

Discussion in 'Apache' started by Weirfire, Aug 22, 2005.

  1. #1
    This is the first time I've used mod rewrite so it might just be because I've done something wrong. Here is my code to get started;

    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule singlearticle/(.*)/(.*)/$ /singlearticle.php?$1=$2
    Code (markup):
    I've placed this in the .htaccess file and mod rewrite is a loaded module on the server.

    Any ideas why this wouldn't turn the URL singlearticle.php?articleid=1 into /singlearticle/articleid/1/?
     
    Weirfire, Aug 22, 2005 IP
  2. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think that you've got it twisted around. The code that you posted should turn /singlearticle/articleid/1/ into singlearticle.php?articleid=1, which is probably what you're aiming for if you're trying to make SE friendly URLs.

    cheers

    John
     
    johnt, Aug 22, 2005 IP
    Weirfire likes this.
  3. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #3
    Well it's basically just to get me started. I really want to turn singlearticle.php?title=thistitle into /article/thistitle but I thought if I used the example above it would be a good place to start.

    Like I said I've never used mod rewrite before :)
     
    Weirfire, Aug 22, 2005 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Lets say I wanted to change domain.com/articles.php?articleid=1 into domain.com/articles/1/ what code would I be wanting to write into the .htaccess file?
     
    Weirfire, Aug 22, 2005 IP
  5. arestia

    arestia Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Options +FollowSymLinks
    RewriteEngine on
    
    RewriteCond %{REQUEST_URI} ^/articles/(.*)/
    RewriteRule ^(.*)/ /articles\.php?articleid=%1  [L]
    Code (markup):
     
    arestia, Aug 22, 2005 IP
    Weirfire likes this.
  6. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think that you've got the code right, you just got the test wrong. If I read your post correctly, you put singlearticle.php?articleid=1 into your browser and it didn't get changed.

    Try using the same .htaccess and putting /singlearticle/articleid/1/ into your browser.
    I think that this is what you're trying to achieve - it will turn an ugly looking URL into something that looks good to both visitors and search engines

    cheers

    John
     
    johnt, Aug 22, 2005 IP
  7. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #7
    This is basically what the first 6 lines of my code looks like

    AddType text/css .css
    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_URI} ^/articles/(.*)/
    RewriteRule ^(.*)/ /singlearticle\.php?articleid=%1 [L]

    and nothing happens if I put the URL in either way round.

    I'll PM you the URL.
     
    Weirfire, Aug 22, 2005 IP
  8. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #8
    Give this a go
    
    Options +FollowSymLinks
    RewriteEngine on
    RewriteRule ^singlearticle/(.*)/?$ /singlearticle.php?articleid=$1
    
    Code (markup):
     
    dct, Aug 22, 2005 IP
    Weirfire likes this.
  9. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #9
    No condition or base?
     
    Weirfire, Aug 22, 2005 IP
  10. dct

    dct Finder of cool gadgets

    Messages:
    3,132
    Likes Received:
    328
    Best Answers:
    0
    Trophy Points:
    230
    #10
    I'm not an expert on this but I muddled my way through on a new site and the above code works a treat for me with a slight variation
     
    dct, Aug 22, 2005 IP
  11. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #11
    Hmm,

    I'm beginning to think that there's something obvious I'm missing out. The wordpress mod rewrite worked but this one isn't.
     
    Weirfire, Aug 22, 2005 IP
  12. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I think dct is right, I think that the RewriteBase directive is screwing it up. Just had a quick glance at the manual again, and I think that the RewriteBase refers to the physical path on the server, not the URL path. Try taking the RewriteBase out and I think it should be fine.

    cheers

    John
     
    johnt, Aug 22, 2005 IP
  13. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #13
    Nintendo, Aug 22, 2005 IP
    Hodgedup likes this.
  14. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #14
    Thanks for all your help guys.

    It's still not working even trying all your solutions. I'm going to try it on a different server just in case it's a server problem. Will post here the results!
     
    Weirfire, Aug 23, 2005 IP
  15. Nintendo

    Nintendo ♬ King of da Wackos ♬

    Messages:
    12,890
    Likes Received:
    1,064
    Best Answers:
    0
    Trophy Points:
    430
    #15
    If you have domain.com/index.html for example, try this...

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^wacko.html$ index.html [L]

    and go to domain.com/wacko.html

    If that URL doesn't work, then you probably don't have .htaccess support. What kind of server are you on? I once tried it on a Windows server, er hacked in to MikeDammann's server (That's sitetutor for those of you in Rio Linda), and it didn't work.
     
    Nintendo, Aug 23, 2005 IP
    Weirfire likes this.
  16. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #16
    Thats strange!

    I tried your example and it didn't work either yet the wordpress rewrites are working properly. According to phpinfo the mod_rewrite module is set up on the server.

    :confused:
     
    Weirfire, Aug 23, 2005 IP
  17. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #17
    Do you need to put any slashes before the ? because its a symbol?
     
    Weirfire, Aug 23, 2005 IP
  18. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #18
    Not in that part of the rule. You only need to escape the special characters in a pattern that you're trying to match, not in the string that you're turning it into
     
    johnt, Aug 23, 2005 IP
  19. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #19
    Thanks john for sorting out the rewriterule.

    At first I thought that the mod rewrite redirects the URL to the rewritten URL. Is this not the case? I can see that when you type both URL's in you get the same page.
     
    Weirfire, Aug 23, 2005 IP
  20. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #20
    mod_rewrite doesn't affect the rewritten URL - as you say you will always be able to access the page by typing into the address what the rewritten URL would be ( domain.com/page.php?param1=1&param2=2 etc ) and it will show the same page as domain.com/1/2.
    As long as you don't put any links anywhere in that format, no one will be any the wiser

    John
     
    johnt, Aug 23, 2005 IP