SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 33 of 33

Thread: Rewrite Rules

  1. #26
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,315
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    matt,

    Too many recent posts have addressed the question of (.*) gobbling up EVERYTHING in the URL to get a question as to why a trailing RewriteRule won't work!

    As usual, though, you'll get rid of your problem by specifying WHICH characters you are looking for (rather than EVERYTHING) by using "([a-zA-Z0-9]+)" to specify letters and digits.

    The same applies to your redirected scripts and their relative links. Either use absolute links OR just use HTML's BASE tag to retain your relative links.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    Updated mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  2. #27
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hello again...i've almost got my first one working

    i was to turn a url like:

    www.38.co.za/clubs/club?id=olde65
    to
    www.38.co.za/clubs/olde65

    i have the following:
    Code:
    Options -MultiViews
    Options +FollowSymLinks
    
    RewriteEngine On
    RewriteRule ([a-zA-Z0-9.])$ club.php?id=$1 
    #RewriteRule ^olde65 club.php?id=olde65
    (the commented out one works...)

    The redirecting is working, but i'm not getting my id variable passed to my script. for some reason it is passing 'p' ...i have no idea where it is getting that from?

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

    Remove the dot from your RewriteRule (otherwise, it should loop) and you should be okay:

    Code:
    RewriteEngine On
    RewriteRule ([a-zA-Z0-9])$ club.php?id=$1 [L]
    Be SURE to put that in the clubs directory's .htaccess, not website root.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    Updated mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #29
    SitePoint Member
    Join Date
    Jan 2005
    Location
    Wales
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having problems with something like this, tried reading up, not sure what to do. Someone suggested I use mod_rewrite but what do I put in it?

    This is how I explained my problem:

    For example, I have a page at http://www.goofyhumor.com/ggcartoons2/index.php/1 but I can't seem to get it to work without the index.php in it like http://www.goofyhumor.com/ggcartoons2/1 - Get a HTTP 404

    I have $var_array = explode("/",$PATH_INFO); in my PHP script then select the information from the database using $var_array[1].

    How do I get it look like http://www.goofyhumor.com/ggcartoons2/1 - do I need extra code, files or need to change some server files?

    As you may have a guessed, I'm not an expert PHP programmer

    Thanks for any help you can give.

  5. #30
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok. tried that:

    so now for a url:
    www.38.co.za/clubs/olde65

    with

    Code:
    RewriteEngine On
    RewriteRule ([a-zA-Z0-9])$ club.php?id=$1 [L]
    i still get $_GET='p'

    with
    Code:
    RewriteEngine On
    RewriteRule ([a-zA-Z0-9]+)$ club.php?id=$1 [L]
    i get $_GET['id'] = php

    ..and i should be getting $_GET['id']= olde65.

    any ideas?

  6. #31
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,315
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    toasti,

    Actually, the code (in the clubs subdirectory) should be

    Code:
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ club.php?id=$1 [L]
    to redirect www.38.co.za/clubs/olde65 to your script at www.38.co.za/clubs/club.php?id=olde65. The difference is the "^" which stipulates that the {REQUEST_URI} string must "start with..." and the "$" specifies that it must be the "end of string."

    The other way (without the "^"), mod_rewrite should look for and skip the last non-conforming character (".") in the {REQUEST_URI} string (clubs.php) and return "php".

    It's beginning to sound like you want mod_rewrite to change the links in your pages to give you the new format URLs -- it doesn't do that. What we're doing is allowing you to utilize "human-friendly" URLs and convert them within Apache for your scripts to use.

    Goofy,

    The same applies to you as you want to change the URL http://www.goofyhumor.com/ggcartoons2/1 to ??? If you want a webroot index.php file to serve the cartoon defined by cat=ggcartoons2 and id=1, then

    Code:
    RewriteEngine On
    RewriteRule ^([a-z0-9]+)/([0-9]+)$ index.php?cat=$1&id=$2 [L]
    will look for one or more lowercase and digits followed by a slash followed by one or more digits and redirect that to the index script with the query string attached. YOU must:

    1. create the links in the new format AND
    2. ensure that links within the script are preserved as ABSOLUTE OR use the HTML BASE tag to identify the actual location of the script.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    Updated mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #32
    SitePoint Guru toasti's Avatar
    Join Date
    Feb 2004
    Location
    Grahamstown
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually, the code (in the clubs subdirectory) should be


    Code:
    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9]+)$ club.php?id=$1 [L]
    thanks dklynn. works like a bomb

  8. #33
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,315
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    matt,

    Sorry, it's too late to go into depth but you should look at replacing your (.*) "catch all" regex with something more specific ([a-z]+) as the "catch all" can capture slashes, dots, EVERYTHING. Normally, that leads to problems.

    G'nite.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    Updated 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
  •