SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite + php issue

    Hello,

    I have the following in an .htaccess:

    Code:
     #rewriteEngine on 
    Options +FollowSymlinks 
    rewriteBase / 
    RewriteRule   ^cms/.*$	  -				  [L]
    RewriteRule   ^documents/.*$	  -				  [L]
    RewriteRule !\.(gif|GIF|jpg|JPG|png|PNG|css|CSS|swf|SWF)$ index.php?
    #Include /include/body/
    ErrorDocument 404 http://www.mysite.com/error.php

    And the code that actually does the work of finding what you are looking for:

    Code:
    $url=strip_tags($REQUEST_URI);
    $url_array=explode("/",$url);
    array_shift($url_array);
    /*
    $category=$url_array[0];
    $document=$url_array[1];
    $documentSQL="SELECT * FROM hay WHERE category='".$category."' AND document='".$document."' AND deleted=0";
    $documentRS=mysql_query($documentSQL,$db);
    if(mysql_num_rows($documentRS) == 0){
     $documentSQL="SELECT * FROM document WHERE label='404' AND visible=0 AND deleted=0";
    $documentRS=mysql_query($documentSQL,$db);
    }
    echo mysql_error();
    */
    What I want to do is have .htaccess add to:

    Code:
    RewriteRule !\.(gif|GIF|jpg|JPG|png|PNG|css|CSS|swf|SWF)$ index.php?
    ...the get variables that may or may not have been sent by the user.

    As it is right now, all get variables are vanishing when apache rewrites.

    Any ideas as to how to solve this.

    Thank you,
    Luis

  2. #2
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I did some modifications to your rules:
    Code:
    Options +FollowSymlinks
    RewriteEngine On
    
    # Every request beginning with cms/ will be skipped
    RewriteRule ^cms/ - [NC,L]
    
    # Every request beginning with documents/ will be skipped
    RewriteRule ^documents/ - [NC,L]
    
    # Every request not having the below extensions will be
    # passed to index.php
    # Here I added the php extension because if a 404 is generated
    # your error.php won't never be called
    RewriteRule !\.(gif|jpe?g|png|css|swf|php)$ index.php [NC,L]
     
    # No needs to specify the full domain path,
    # use http://etcetgc only if you want to do an external redirection
    ErrorDocument 404 /error.php
    The above rules will take care of query strings.

    So calling something like
    /path/to.html?name=pippo
    it will call internally index.php and $_GET[ 'name' ] will contain 'pippo'

    You wrote index.php?,
    basically adding the ? you are telling to the rewrite parse to call index.php without the query string.
    That was the reason why you didn't have query string parameters passed to index.php.

    Also take a look on what I wrote about error.php.


    :-) pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  3. #3
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow... perfect!

    It works gtreat...

    Grazie!!
    Luis

  4. #4
    SitePoint Addict
    Join Date
    Mar 2002
    Location
    Miami, Florida - Caracas, Venezuela
    Posts
    379
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What do the "[NC,L]" mean?

    Where can I find .htaccess documentation (in particular mod_rewrite)?
    Luis

  5. #5
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    NoCase (case insensitive), Left (exit from rewrite parser)

    The flags are explained here:
    http://httpd.apache.org/docs/mod/mod_rewrite.html
    and some examples are found here:
    http://httpd.apache.org/docs/misc/rewriteguide.html

    Remember to check the resources collection too (check my sig).


    :-) pippo
    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03


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
  •