Help me with mod rewrite - $30 for the solution

Discussion in 'Apache' started by rcastro, Aug 4, 2007.

  1. #1
    Hello,

    I try to use this code:

    Options +FollowSymLinks
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^search/([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([^/]*)/?([0-9]*)/?([0-9]*)/?([0-9]*)/?([0-9]*)/?([0-9]*)/?$ /search/search.php?variable1=$1&variable2=$2&variable3=$3&variable4=$4&variable5=$5&variable6=$6&variable7=$7&variable8=$8&variable9=$9&variable10=$10 [QSA,L]
    Code (markup):

    The last 5 varibles are numers. I know that using this rule i can only have variable $1 to $9, variable $10 is considered $1....

    I use $_GET to get the variables from the search form.

    I want to urls be like this:

    domain.com/search/variable1/variable2/variable3/

    or

    domain.com/search/variable3/variable7/variable8/

    or

    domain.com/search/variable2/variable4/variable9/variable10

    or

    domain.com/search/variable1/variable2/variable3/variable4/variable5/variable6/variable7/variable8/variable9/variable10

    and so on...

    This shouldn't happen:
    domain.com/search///variable3////variable7/variable8///
    _________________
     
    rcastro, Aug 4, 2007 IP
  2. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Instead of trying to do all this using mod_rewrite, you'd likely be better off parsing the PATH_INFO yourself within your search.php.
    This will skip all the mod_rewrite hassle, and give you the desired result with more control over the URL.

    To do this, rename "search.php" to just "search" (you can also symlink search -> search.php).

    Then, in your .htaccess file, make sure "search" uses PHP:
    
    AcceptPathInfo On
    
    <Files "search">
         AddType application/x-httpd-php
    </Files>
    
    Code (markup):
    The above should work for most Apache servers, but may need tweaking depending on your Apache / PHP configuration.

    Then, at the top of your search.php:
    
    <?php
    if (isset($_SERVER['PATH_INFO'])) {
         $pathInfo = explode("/", $_SERVER['PATH_INFO']);
         $numPathInfo = count($pathInfo);
    
         if ($numPathInfo > 11)
              $numPathInfo = 11;
    
         for ($i = 1; $i < $numPathInfo; $i++) {
              $_GET['variable' . $i] = $pathInfo[$i];
         }
    }
    ?>
    
    PHP:
    This will populate $_GET['variable1' ... 'variable10'] as you requested.
    To add more variables, just remove the "if ($numPathInfo > 11)".
     
  3. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hello bilal@revolutionhosting,

    Thx for your help.

    But if i had

    AcceptPathInfo On

    <Files "search">
    AddType application/x-httpd-php
    </Files>

    this to .htaccess i got Internal Server Error (404)
     
    rcastro, Aug 4, 2007 IP
  4. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You shouldn't be getting a 404 Not Found.
    Did you rename "search.php" to "search", or symlink search -> search.php?

    EDIT
    My Apache configuration directives are a little rusty!
    Instead of "AddType", use "ForceType".
     
  5. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes i rename search.php to search

    My .htaccess file:

    
    AcceptPathInfo On
    
    <Files "search">
         ForceType application/x-httpd-php
    </Files>
    
    Code (markup):
    My search file:

    <?php echo "HELLO WORLD"; ?>
    Code (markup):
    I still get 404 error
     
    rcastro, Aug 4, 2007 IP
  6. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Please try checking your Apache error_log.

    Regardless, you should not be getting "404 Not Found" when you have a file by that name. Even if it's not being correctly interpreted by PHP, you would not get a 404 error.
     
  7. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #7

    This is the error, it's a 500 error + 404 error

    Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.
    
    Please contact the server administrator, webmaster@domain.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
    
    More information about this error may be available in the server error log.
    
    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request. 
    Code (markup):
     
    rcastro, Aug 4, 2007 IP
  8. bilal@revolutionhosting

    bilal@revolutionhosting Peon

    Messages:
    32
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ah, a 500 Internal Server Error makes some sense at least! ;)

    Can you paste the message shown in your Apache error_log?
    There may be an issue with the .htaccess directives I gave you.
     
  9. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    The error_log is empty.
     
    rcastro, Aug 4, 2007 IP
  10. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #10
    AcceptPathInfo is only available in Apache 2.0.30 (and later). Maybe check the version of your Apache?

    If it's not 2.0.30 or later, then your .htaccess would be:
    
    <Files "search">
    ForceType application/x-httpd-php
    </Files>
    
    Code (markup):
    And the search file:
    
    <?php
    if (isset($_SERVER['PATH_INFO'])) {
    	$pathInfo = explode("/", $_SERVER['PATH_INFO']);
    
    	if (end($pathInfo)) $numPathInfo = count($pathInfo);
    	else $numPathInfo = count($pathInfo) -1;
    
    	for ($i = 1; $i < $numPathInfo; $i++) {
    		$_GET['variable' . $i] = $pathInfo[$i];
    	}
    }
    ?>
    
    PHP:
     
    VimF, Aug 4, 2007 IP
  11. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #11
    The apache version is 1.3.37

    I used this code on .htaccess and the rest of site works fine but if i access to the search file i get a 500 error

    
    <Files "search">
    ForceType application/x-httpd-php
    </Files>
    
    Code (markup):
     
    rcastro, Aug 4, 2007 IP
  12. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #12
    Hmm... it works for me. Maybe your server configuration doesn't allow that.

    Well, you might want to use "search.php" (or "search.whatever") instead of just "search", like:
    domain.com/search.php/variable1/variable2/variable3/

    EDIT:
    An alternative way to archive a "search":
    Options FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^search/(.+)$ /search/search.php/$1 [L,NC]
    Code (markup):
     
    VimF, Aug 4, 2007 IP
  13. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Using

    Options +FollowSymLinks
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^search/(.+)$ /search/search.php/$1 [L,NC]
    Code (markup):
    It works...i'm already using that for other files.

    After i submit the search query my url looks like this:

    search.php?variable1=something1&variable2=something2&variable3=something3&variable4=something4&variable5=something5...

    and that code doesn't help me
     
    rcastro, Aug 4, 2007 IP
  14. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #14
    Okay, add something likes this to your search.php:

    
    if ($_GET['variable1']) $var1 = '1,'.$_GET['variable1'].'/';
    if ($_GET['variable5']) $var5 = '5,'.$_GET['variable5'].'/';
    if ($_GET['variable8']) $var8 = '8,'.$_GET['variable8'].'/';
    $path = $var1.$var5.$var8;
    
    if (preg_match('/\.php/i', basename($_SERVER['REQUEST_URI']))) {
    	header('HTTP/1.1 301 Moved Permanently');
    	header('Location: /search/'.$path);
    	exit();
    }
    
    PHP:
    After submitting your URL will be like:
    domain.com/search/1,something1/5,something5/8,something8/

    Then use this in your search.php to get the variables:
    
    if (isset($_SERVER['PATH_INFO'])) {
    	$pathInfo = explode("/", $_SERVER['PATH_INFO']);
    
    	if (end($pathInfo)) $numPathInfo = count($pathInfo);
    	else $numPathInfo = count($pathInfo) -1;
    
    	for ($i = 1; $i < $numPathInfo; $i++) {
    		$vars = explode(",", $pathInfo[$i]);
    		$_GET['variable'.$vars[0]] = $vars[1];
    	}
    }
    
    PHP:
     
    VimF, Aug 4, 2007 IP
  15. rcastro

    rcastro Peon

    Messages:
    61
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #15
    VimF,

    That's work, but I still want the url more simple

    like domain.com/search/variable1/variable2 .... or domain.com/search/variable1,variable2,variable3
     
    rcastro, Aug 4, 2007 IP
  16. VimF

    VimF Well-Known Member

    Messages:
    307
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    118
    #16
    Well, that can't be more simple.

    Let's say you want to input 3 variables:

    variable1 = blah
    variable2 = null
    variable3 = blah

    For the way you want it:
    domain.com/search/variable2/variable4/variable9/variable10
    The URL will be like this:
    domain.com/search/blah/blah or domain.com/search/blah,blah

    Now how does the server know if variable2 is null and the 2nd blah is for variable3?...

    You will need something to identify them, something like:
    domain.com/search/1,blah/3,blah or domain.com/search/1~blah,3~blah
     
    VimF, Aug 4, 2007 IP