SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with Mod_Rewrite

    Hello all,

    I have problem here to can convert my script pages to Mod_Rewrite system

    i wana convert
    PHP Code:
    category.php?cID=
    To

    PHP Code:
    category-1.html
    or
    category/1.html 
    Thanks

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello;
    Firstly, you need to put this into your .htaccess

    Code:
    RewriteEngine on
    RewriteRule !\.(gif|jpg|png|css)$ /path/to/file
    (Obviously replace path/to/file with the actual location, the rewrite rule is if you want everything to be accessed from one file, like index.php)

    and then you will need to write a php script that takes the requested uri, and explode()s it at "/", so if a user requests:

    ..../category/food/fruit/citrus

    then you will hav an array similar to (depending on how you would apply this):

    Code:
    Array (
        [0] => 'category'
        [1] => 'food'
        [2] => 'fruit'
        [3] => 'citrus'
    )
    And now you can manipulate that as you wish.

    Since you want to make friendly urls, you may want to consider using descriptive categories, instead of "1".

    Hopefully this will give you an idea on how this works!

  3. #3
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On the other hand you could just do a specific rule...
    Code:
    RewriteCond %{REQUEST_URI} ^/category/([0-9]+)/?$
    RewriteRule .* /category.php?cID=%1 [L,QSA]
    This rewrites /category/1/ (or /category/1 ) to /category.php?cID=1

    Additional advantage of this is that only numbers will get past the regex if somebody writes /category/hack/ it will result in a page not found.

    use ([^/]+) to match any character.

    QSA appends any additional query strings.

  4. #4
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks fjook for help me but i am beginner, and i need easy way to make this result,
    any other easy method for that

  5. #5
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Will Kelly

    I test your method and i changed link
    and i make .htaccess file like your file

    from
    PHP Code:
    <a href="category.php?cID=<?=$cID?>"><?=$catName?></a>
    to
    PHP Code:
    <a href="category/<?=$cID?>"><?=$catName?></a>
    but not worked it give me "The page cannot be found"

    and on page category.php can i change any thing on query?
    PHP Code:
    $catz mysql_query("select cID,catName from categories WHERE cID='$_GET[cID]'"); 
    i dont know why

  6. #6
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i had a beginning slash for starting at the root of the website. Take that off if you're not in the root. Don't forget to add 'RewriteEngine on' in the .htaccess file as well.

  7. #7
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Will Kelly for your help

    i try all thing on .htaccess file like :

    PHP Code:
    Options +FollowSymLinks
    RewriteEngine on

    RewriteCond 
    %{REQUEST_URI} ^/category/([0-9]+)/?$
    RewriteRule .* /category.php?cID=%[L,QSA
    and

    PHP Code:
    Options +FollowSymLinks
    RewriteEngine on

    RewriteCond 
    %{REQUEST_URI} ^category/([0-9]+)/?$
    RewriteRule .* category.php?cID=%[L,QSA
    and all results is "The page cannot be found"

    i test it on my local host and me webhost and not worked i dont know why

    Thanks

  8. #8
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try it like this? (though this never works on my server for some reason)..

    Code:
    RewriteRule ^category/([0-9]+)/?$ category.php?cID=%1 [L,QSA]
    also where is your .htaccess file in relation to your php file? (it should ideally be in the same directory).

  9. #9
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah .htaccess file on same folder with php file

    this code working fine on my webhost

    but not working on localhost

    ok but now i have small problem on category.php i make query

    PHP Code:
    $catz mysql_query("select cID,catName from categories WHERE cID='$_GET[cID]'"); 
    now when i go to to page like http://localhost/demo/category/3 it give me empty page not give me any results and when i try http://localhost/demo/category.php?cID=3 give me results fine.

    i ask about can i make any change on this code to can script work fine now.

    and iam sorry for all this questions

    thanks

  10. #10
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you said it wasn't running on localhost anyway, does that work on your webhost?

  11. #11
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah it work fine on webhost.

    but i ask now how to use variables on this case

    thanks

  12. #12
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help more

    all thing is great but what can i do now for variables.

    PHP Code:
    $catz mysql_query("select cID,catName from categories WHERE cID='$_GET[cID]'"); 
    Thats query working fine when i use like
    PHP Code:
    category.php?cID=
    but not working on
    PHP Code:
    category/1
    thanks

  13. #13
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any help in this case

    Thanks

  14. #14
    SitePoint Evangelist Will Kelly's Avatar
    Join Date
    May 2005
    Location
    London
    Posts
    475
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As long as it works the regex rewrites the url to the script 'category.php?cID='. i t does not replace your original script.

    You said you had it working on your webhost?, I assume your localhost can't do url-rewrites (for whatever reason).

  15. #15
    SitePoint Zealot php.net's Avatar
    Join Date
    Oct 2003
    Location
    php.net
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks Will Kelly

    I tell you now when i open
    PHP Code:
    www.domain.com/category/1
    not give me any variables on this page, it give me empty page! , i used
    PHP Code:
    cID='$_GET[cID]' 
    on my query

    thats my qustion

    and thanks for your help again


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
  •