Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Anyone using apache's ModRewrite? Examples

    I'm looking into some information about using ModRewrite to make my dynamic URLS (ie, article.php?S=1&A=5) a little more visually appealing (not to mention, search engine/favourites/bookmark friendly).

    I'd like some advice from people who have used ModRewrite before and some examples of how you did it.

    Also, if this is in the wrong forum, Mods, please move this topic

    Thanks!

    IC

  • #2
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Check it out:
    Code:
    RewriteRule ^site/([a-zA-Z0-9]+)[/]?([a-zA-Z0-9]+)?[/]? /?c=$1&s=$2 [QSA]
    That turns an address like "/site/articles/javascript/" into "?c=articles&s=javascript", and using the "magic" QSA flag (which stands for Query String Append) you can still use CGI parameters at the end of your clean URLs, and they'll get copied seamlessly into the final GET request

    Adding further sections is as simple as adding more replacement expressions - on my site I go up to 5 levels deep:
    Code:
    RewriteRule ^site/([a-zA-Z0-9]+)[/]?([a-zA-Z0-9]+)?[/]?([a-zA-Z0-9]+)?[/]?([a-zA-Z0-9]+)?[/]?([a-zA-Z0-9=&]+)? /?c=$1&s=$2&z=$3&m=$4&$5 [QSA]
    Ultimately, using ModRewrite is all about having a good grasp of regular expressions - are you happy with that, or would you like some links?
    Last edited by brothercake; 05-12-2005 at 10:25 PM.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    here's a nice and simple tutorial

    http://www.oxyscripts.com/item-1052.html

  • #4
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by brothercake
    Ultimately, using ModRewrite is all about having a good grasp of regular expressions - are you happy with that, or would you like some links?
    I think I'm pretty happy with that explaination. Thanks alot Brothercake!

    I am familiar with using regular expressions, although I must admit, I'm out of practice, so it'd take a little bit for me to get back up to speed on them.

    Can you give an example of how I would use just numbers instead of words?

    Thanks, I really appreciate your insight.

    IC

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    example....

    turn domain.com/articles/2/12/4/ into domain.com/article.php?S=2&A=12&SA=4

    the final slash I guess would be optional (so it could be articles/2/12/4/ or articles/2/12/4)

    Thanks again

    IC

  • #6
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, do I have to include "RewriteEngine on" before my rewrite conditions?

    That might be why it doesn't work...

    If you want to just post your entire .htaccess file, that might help! :P

    Thanks again

    IC

  • #7
    Regular Coder
    Join Date
    Jul 2003
    Location
    Ontario, Canada
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    hmm

    I seem to have it working sort of... however, the stylesheet isn't loading in.

    example..

    Code:
    RewriteRule ^articles[/]?$ /article.php
    RewriteRule ^articles/([0-9]+)[/]?$ /article.php?S=$1
    If the user types in domain.com/articles the stylesheet loads fine.
    if they type in domain.com/articles/ the stylesheet doesn't load.

    Also, if they go one step further and type in domain.com/articles/1 (or 1/) the stylesheet also does not load.

    Do I need to use absolute addressing for everything now? (ie, do I need to use /images/logo.gif rather than images/logo.gif ?)

    Help! Please!

    Thanks

    IC
    Last edited by IdentityCrisis; 05-13-2005 at 09:03 PM.

  • #8
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by IdentityCrisis
    Do I need to use absolute addressing for everything now? (ie, do I need to use /images/logo.gif rather than images/logo.gif ?)
    Well no, but it would be far simpler. Say, for example, your page at "/index.php" gets addressed as "/articles/whatever/" the relative path to "/images/" would then be "../../images/"

    Part of the benefit of a dynamic addressing system is that your site topography and folder structure don't have to match - you can organise either independently of the other - so you really do wanna use base-absolute paths or you'll have lost that benefit.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

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