SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Mod-rewrite in apache

    I saw this question every where and found articles and websites...
    but never understood what's the rule , where or how to excute?
    I've apache server running on Linux (I don't know anything about linux)
    need to know in a very few steps how to do mod-rewriting..
    example: this page
    http://www.anwarica.com/egypt/egypt.php?cat=water
    I need it to be:
    http://www.anwarica.com/egypt/egypt/water
    or any other possible form!
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

  2. #2
    Forums Designer
    Join Date
    Jun 2004
    Posts
    422
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    RewriteEngine on
    RewriteRule ^/(.*)/([a-z0-9]*) /$1.php?cat=$2

    And you might need to add your homepage path to it like this (depending on server configuration):
    RewriteEngine on
    RewriteRule ^/home/users/whatever/public_html/(.*)/([a-z0-9]*) /$1.php?cat=$2

  3. #3
    King of da Wackos Nintendo's Avatar
    Join Date
    Nov 2003
    Location
    Planet Zeekois
    Posts
    1,182
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.domain.com/.htaccess

    Options +Indexes
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    RewriteRule ^egypt/(.*)$ egypt/egypt.php?cat=$1 [L]


    http://www.domain.com/egypt/WHATEVER
    ...

  4. #4
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys,
    But I never understood those
    Code:
    RewriteEngine on
    RewriteBase /
    RewriteRule ^egypt/(.*)$ egypt/egypt.php?cat=$1 [L]
    I mean what's the Engine? where I put that code? in my pages or in specific files in servers
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

  5. #5
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Moved to the right forum. Here's the right code: It goes in your .htaccess file in the domain root folder.

    Code:
    RewriteEngine On
    RewriteRule ^egypt/([a-z]+)(/([a-z]+)?/?$ egypt/$1.php?cat=$3 [NC,L]
    http://www.anwarica.com/egypt/egypt/water/ will go to http://www.anwarica.com/egypt/egypt.php?cat=water
    http://www.anwarica.com/egypt/egypt/ will go to http://www.anwarica.com/egypt/egypt.php?cat=

    etc...
    Mike
    It's not who I am underneath, but what I do that defines me.

  6. #6
    ********* Genius Mike's Avatar
    Join Date
    Apr 2001
    Location
    Canada
    Posts
    5,458
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You may prefer this way though:

    Code:
    RewriteEngine On
    RewriteRule ^egypt/(water|old|modern|arts|hotel|fav)/?$ egypt/egypt.php?cat=$1 [NC,L]
    Took the nav off your site for this one. Use like so:

    http://www.anwarica.com/egypt/water
    http://www.anwarica.com/egypt/fav
    http://www.anwarica.com/egypt/hotel

    etc...
    Mike
    It's not who I am underneath, but what I do that defines me.

  7. #7
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Mike!
    I'm trying them, thanks also for moving (I didn't expect this section about apache) in addition there are many threads about Mod_rewrite here!
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

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

    You've done an excellent job in writing the proper code for Tri! Hard to believe that you're claiming that you've just started in mod_rewrite only two weeks ago - shows what you can do with an open mind and a desire to learn!
    [aside]Okay, I preferred your previous avatar![/aside]
    Tri,

    There is an article at datakoncepts.com/seo that would have shown you most (if not all) of this so it might be both a good review and a full explanation of what Mike has done for you - that understanding is actually MORE important than having it done for you.

    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

  9. #9
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's working 100%
    Thanks DK for the article it will help me to understand the dissection of the code..
    yet I'm thinking about the next pages,
    example:
    http://www.anwarica.com/egypt/egypt.php?go=4&cat=water
    can it be like:
    http://www.anwarica.com/egypt/water/4
    N.B. please let me handle it for days just if I didn't reply before 29 May!
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

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

    If your last statement (the N.B.) meant not to help, my apologies - DON'T READ FURTHER (until 20 May)!

    Now, I didn't go back to Mike's code so there may be some conflict but, to redirect http://www.anwarica.com/egypt/water/4 to http://www.anwarica.com/egypt/egypt.php?go=4&cat=water, all you will need to do is:
    Code:
    RewriteEngine On
    RewriteRule ^egypt/([a-z]+)/([0-9]+)$ egypt/egypt.php?go=$2&cat=$1 [L]
    # OR 
    # RewriteRule ^egypt/(water|old|modern|arts|hotel|fav)/([0-9]+)$ egypt/egypt.php?go=$2&cat=$1 [L]
    Of course, the second rule uses the same type of atom with optional choices to replace the cat OR open the [a-z] specification if you're to include UPPERCASE or _, and change the go value to something else if it's not supposed to be one or more digits.

    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

  11. #11
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    Quote Originally Posted by dklynn
    Tri,

    If your last statement (the N.B.) meant not to help, my apologies - DON'T READ FURTHER (until 20 May)!
    No worry I didn't read further, just till 29th May I'll complete reading (as I test my coding )
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

  12. #12
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    29th May 2005
    I understood some, but couldn't apply on my localhost as apache isn't working (so I use iis)!
    Yet, I tried on apache (website) but couldn't make pages by Dk's code..
    so I made a remix
    Code:
    RewriteEngine On
    RewriteRule ^egypt/(water|old|modern|arts|hotel|fav)/?$ egypt/egypt.php?go=4&cat=$1 [NC,L]
    but didn't work
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

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

    Given that you want to use the {REQUEST_URI} string egypt/water/4, your regex will NOT work 'cause you've prohibited it from working because of the "4"

    RewriteRule ^egypt/(water|old|modern|arts|hotel|fav)/?$ egypt/egypt.php?go=4&cat=$1 [NC,L]

    RewriteRule is the directive

    The regex is ^egypt/(water|old|modern|arts|hotel|fav)/?$ which looks for a string STARTING with egypt/ then one of the group within the parentheses. A trailing slash MAY follow but then the string MUST END.

    The redirect string is egypt/egypt.php?go=4&cat=$1 with $1 being the one item within the parentheses that was found.

    What to do? Change your rule to:

    RewriteRule ^egypt/(water|old|modern|arts|hotel|fav)/([0-9]+)/?$ egypt/egypt.php?go=$2&cat=$1 [NC,L]

    which looks for one or more digits following the list item and allows an optional trailing slash. That should do it for you for Apache. I'm not sure about IIS but I don't believe Mr Gates wants you to do redirects.

    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

  14. #14
    Life is strife TriGeminal's Avatar
    Join Date
    Apr 2005
    Location
    Trigeminal Ganglion
    Posts
    633
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks DK, it's working
    I'll try to fix my localhost to be quicker in learning too..

    Quote Originally Posted by dklynn
    I'm not sure about IIS but I don't believe Mr Gates wants you to do redirects.
    They use ISAPI modules and have a complicated manners as usual
    "The only thing necessary for the triumph of evil ..
    .. is for good men to do nothing"
    Edmund Burke.

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

    Does that mean Mr Gates is applying Apache coding in IIS? Oh, well, I believe it wouldn't be the first code that did not originate in Redmond.

    Although I prefer to keep away from anything Micro$oft online, can you please provide a link where I can look at IIS details to I can help IIS people here, too? THANKS!

    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
  •