Page 1 of 1

Best php url re-write.

Posted: Thu Jun 24, 2010 3:17 am
by aravona
I have a website which needs 'pretty' urls.

However the re-write which was put in originally really messed up the website. As in, the links actually no longer went to where they needed to go.

The URLs will be based on a mysql table field which is simply 'name'.

The URLs need to br re-written from /index.php?cmd=32 etc etc to /name-from-table/

I've been looking into doing this and it is a bit beyond my php level, is php the only option for this? The current re-write which works as in it re-writes the files, but doesnt actually send you to any pages....

I'm still using good old google to try and figure this out but any help or direction would be really really appriciated.

Ara

Re: Best php url re-write.

Posted: Thu Jun 24, 2010 4:11 am
by davex
Hi,

Just to be a pedant I presume you mean you want to rewrite /name-from-table/ to index.php?cmd=32 rather than the other way round?

You can do this entirely with PHP - probably the best solution would be to set a 404 page not found handler to a PHP script which looked up the URL needed and either just did a straight redirect (which would change the browsers address bar) or itself just loaded as the correct page.

The downside of this is that 404 errors are often caught by the browsers (especially IE) and so you need to output some data etc.

Personally I think the most common way of doing this is to use Apache's rewrite engine (assuming you are using apache) - http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html.

In this way you can easily rewrite say /name-from-table/ to go to index.php?name=name-from-table and then just in the index.php do the lookup to get the relevant "cmd number" and display appropriately. This is invisible to the user who still sees /name-from-table/

HTH,

Cheers,

Dave.

Re: Best php url re-write.

Posted: Thu Jun 24, 2010 4:27 am
by aravona
Ok what I mean is currently the page shows:

'site.co.uk/index.php?cmd=32' and I need it to say 'site.co.uk/name-from-table/'

Like I said, I need 'pretty' urls. However the current re-write isnt linking to the right pages.

For example 'site.co.uk/index.php?cmd=32' links to home.php in the switch which 'cmd' relates to. However the current re-write will also send 'site.co.uk/index.php?cmd=44' to the same page when it need to be say page1.php - again set in the switch. With the current re-write it seems to be ignoring the cmd switch entirely. Which is why I wanted a simple way to pull the name from the table and re-write the end of the url with it :)

Re: Best php url re-write.

Posted: Thu Jun 24, 2010 4:32 am
by davex
Hi,

So the rewrite is sort of working e.g. instead of a 404 error for /name-from-table/ you do get the index page but the wrong page shown?

Maybe just disable the switch for the minute and get it to just echo the cmd value it has looked up?

What are you using to perform the rewrite to index.php and what is that code?

Cheers,

Dave.

Re: Best php url re-write.

Posted: Thu Jun 24, 2010 4:36 am
by aravona
Unfortunately its not my code, I just have to fix it. My personal nightmare. The re-write file is as follows:

Code: Select all

<?php
    if(isset($_REQUEST['URL_Text'])){
     		
    	$_REQUEST['cmd']=18;
    	
        $SUBCat_NaMe=explode("-",$_REQUEST['URL_Text']);
        $lastValue=$SUBCat_NaMe[sizeof($SUBCat_NaMe)-1];
        
        for($i=0;$i<sizeof($SUBCat_NaMe);$i++){
    		if($WHEre==""){
    			$WHEre.=" name LIKE '".$SUBCat_NaMe[$i]."%'";
    		}else if($i==sizeof($SUBCat_NaMe)-1){
    			$WHEre.=" AND name LIKE '%".$SUBCat_NaMe[$i]."'";
    			//$WHEre.=" AND subcategory LIKE '%".$SUBCat_NaMe[$i]."'";
    		}else{
    			$WHEre.=" AND name LIKE '%".$SUBCat_NaMe[$i]."%'";
    		}
        }
        if($_REQUEST['URL_Text']=='new-items'){
         
            $_REQUEST['cmd']=5;
            
        }else{
            $query = "SELECT pID FROM ver_parent WHERE $WHEre";
            $result_SET = $class->ResultSet($query);
            $row_SET = $class->FetchObject($result_SET);
            $_REQUEST['CID']=$row_SET->pID;
        }
        
    }////end of if.
    
    if(isset($_REQUEST['URL_Text1'])){
 		$WHEre='';
    	$SUBCat_NaMe=explode("-",$_REQUEST['URL_Text1']);
        $lastValue=$SUBCat_NaMe[sizeof($SUBCat_NaMe)-1];
        
        for($i=0;$i<sizeof($SUBCat_NaMe);$i++){
    		if($WHEre==""){
    			$WHEre=" name LIKE '".$SUBCat_NaMe[$i]."%'";
    		}else if($i==sizeof($SUBCat_NaMe)-1){
    			$WHEre.=" AND name LIKE '%".$SUBCat_NaMe[$i]."'";
    			//$WHEre.=" AND subcategory LIKE '%".$SUBCat_NaMe[$i]."'";
    		}else{
    			$WHEre.=" AND name LIKE '%".$SUBCat_NaMe[$i]."%'";
    		}
        }
        $query = "SELECT cID FROM ver_child WHERE $WHEre";
        $result_SET = $class->ResultSet($query);
        if($class->NumRows($result_SET)!=0){
        
            $_REQUEST['cmd']=2;
            $row_SET = $class->FetchObject($result_SET);
            $_REQUEST['SCID']=$row_SET->cID;
            
        }else{

            $query = "SELECT prID FROM ver_pro WHERE $WHEre";
            $result_SET = $class->ResultSet($query);
            $_REQUEST['cmd']=7;
            $row_SET = $class->FetchObject($result_SET);
            $_REQUEST['pr']=$row_SET->prID;
            
        }
        
    }////end of ......
    
    
    if(isset($_REQUEST['URL_Text2'])){
 		$WHEre='';
    	$SUBCat_NaMe=explode("-",$_REQUEST['URL_Text2']);
        $lastValue=$SUBCat_NaMe[sizeof($SUBCat_NaMe)-1];
        
        for($i=0;$i<sizeof($SUBCat_NaMe);$i++){
    		if($WHEre==""){
    			$WHEre=" name LIKE '".$SUBCat_NaMe[$i]."%'";
    		}else if($i==sizeof($SUBCat_NaMe)-1){
    			$WHEre.=" AND name LIKE '%".$SUBCat_NaMe[$i]."'";
    			//$WHEre.=" AND subcategory LIKE '%".$SUBCat_NaMe[$i]."'";
    		}else{
    			$WHEre.=" AND name LIKE '%".$SUBCat_NaMe[$i]."%'";
    		}
        }
    
        $query = "SELECT prID FROM ver_pro WHERE $WHEre";
        $result_SET = $class->ResultSet($query);
        $_REQUEST['cmd']=9;
        $row_SET = $class->FetchObject($result_SET);
        $_REQUEST['pr']=$row_SET->prID;
        
    }////end of ......

?>
And this links to the following on the index.php:

Code: Select all

<?php require "include.php";
if($_REQUEST['cmd']==18 || $_REQUEST['cmd']==7 || $_REQUEST['cmd']==9 || $_REQUEST['cmd']==10 || $_REQUEST['cmd']==11 || $_REQUEST['cmd']==2 || $_REQUEST['cmd']==3 || $_REQUEST['cmd']==4 || $_REQUEST['cmd']==5){
    if($_REQUEST['cmd']==7 || $_REQUEST['cmd']==9 || $_REQUEST['cmd']==10 || $_REQUEST['cmd']==11){
        
        $query_get="SELECT pID,name FROM ver_pro WHERE prID=$_REQUEST[pr]";
        $result_get=$class->ResultSet($query_get);
        $row_get=$class->FetchObject($result_get);
        $bg_url=$Purl.$class->ReplaceSpaces($class->returnName("name","ver_parent","pID",$row_get->pID,'')).'/'.$class->ReplaceSpaces($row_get->name).'/';
    }
    
    if($_REQUEST['cmd']==18 || $_REQUEST['cmd']==2 || $_REQUEST['cmd']==3 || $_REQUEST['cmd']==4) {
        //http://projects.innova8ive.com/babygifts/index.php?cmd=18&CID=1
        //http://projects.innova8ive.com/babygifts/index.php?cmd=2&CID=62
        //http://projects.innova8ive.com/babygifts/index.php?cmd=3&CID=65
        //http://projects.innova8ive.com/babygifts/index.php?cmd=4&CID=7
        
        $query_get="SELECT pID,name FROM ver_parent WHERE pID=$_REQUEST[CID]";
        $result_get=$class->ResultSet($query_get);
        $row_get=$class->FetchObject($result_get);
        $bg_url=$Purl.$class->ReplaceSpaces($row_get->name).'/';
        
        if(@$_REQUEST['SCID']!=''){
            $query_get1="SELECT cID,name FROM ver_child WHERE cID=$_REQUEST[SCID]";
            $result_get1=$class->ResultSet($query_get1);
            $row_get1=$class->FetchObject($result_get1);
            $bg_url=$bg_url.$class->ReplaceSpaces($row_get1->name).'/';
        }
        //
        //$bg_url=$Purl.$class->ReplaceSpaces($class->returnName("name","ver_parent","pID",$row_get->CID,'')).'/'.$class->ReplaceSpaces($row_get->name).'/';
    }
    
    if($_REQUEST['cmd']==5){
        $bg_url=$Purl.'new-items/';
    }
    
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    Header( "Location: ".$bg_url); 
}
require "rewrite.php";
I get what/how its re-writing but I don't see why its not doing it properly.

The site is this site here. The problem is, if you click in anything in the 1st block of the left hand navigation, it goes to its own home.php... but if you click in an option in the 2nd block... it goes to the same home.php (its split based on category)

Re: Best php url re-write.

Posted: Thu Jun 24, 2010 10:54 am
by ell0bo
First off, you have mysql injection vulnerabilities there. You might want to fix that.

You will meed to change the hrefs your system is using and change then how this indexing is working. It won't be fun...

if you have <a href="/something/page1"> the browser will show /something/page1, but using URL rewrite you can translate that to /something?page=page1 on the server side. What you're trying to do however will require changing the hrefs and some other magical deliciousness. There may be a PEAR library that can help you out, but unfortunately what you're trying to do comes down to bad design and there is no magic pill.

Re: Best php url re-write.

Posted: Fri Jun 25, 2010 1:32 am
by aravona
I have already said it wasn't my code.

The point of the post was more to find a better way to do this. Is my best option doing a htaccess re-write for each and every seperate link rather than this messed up php function ?

EDIT: Never mind, realised it cannot be a htaccess because it needs to be done for every new product dynamically hence why I think the above code was attempted.

Re: Best php url re-write.

Posted: Fri Jun 25, 2010 6:51 am
by ell0bo
Yes, I understand it isn't your code, but I just thought I'd mention it. Mod_Rewrite isn't useful here because you need to actually change the hrefs. Now there may be some kind of like sanitizer you can run it through, but that would require parsing through the html code and it would be costly in a processing sense. If you're able to change how the links are generated, then that's how you need to approach this, otherwise, IMHO, you're SOL.

Re: Best php url re-write.

Posted: Fri Jun 25, 2010 7:14 am
by aravona
ell0bo wrote:Yes, I understand it isn't your code, but I just thought I'd mention it. Mod_Rewrite isn't useful here because you need to actually change the hrefs. Now there may be some kind of like sanitizer you can run it through, but that would require parsing through the html code and it would be costly in a processing sense. If you're able to change how the links are generated, then that's how you need to approach this, otherwise, IMHO, you're SOL.
I'm afraid I have no idea what SOL means. Ontop of that I had already edited the post to say it was not worth using a htaccess.

Also I think what you're suggesting in you're 'how the links are generated' is exactly what whoever did this before me tried. Anyway, told my friend its a no go unless he wants to contact whoever did it first time around.