Forum Moderators: coopster

Message Too Old, No Replies

RewriteRules work, but can't get PHP code right

Search engine friendly URL PHP code

         

seodave

2:53 pm on Oct 20, 2006 (gmt 0)

10+ Year Member



Working on a PHP script that originally used dynamic URLs, trying to make the URLs search engine friendly, but run into a problem when I try to replace spaces and other unwanted characters with hyphens (or remove them).

Using urlencode works, it replaces spaces with a + but it results in some character codes like % 26 for & that don't work in a browser with this script. So pretty sure my RewriteRules are correct, for example this set-


RewriteRule ^([^/]+)/Artist/([^/]+)/$ search.php?typ=$1&search=$2 [L,NC]
RewriteRule ^([^/]+)/Artist/([^/]+)/([^/]+)/$ search.php?typ=$1&search=$2&page=$3 [L,NC]

Results in URLs-

domain.com/word1+word2/Artist/artist+name/
domain.com/word1+word2/Artist/artist+name/1/

When using urlencode and-

domain.com/word1%20word2/Artist/artist%20name/
domain.com/word1%20word2/Artist/artist%20name/1/

When using nothing to convert spaces.

If I add a function like-


function safeurl($name){
$retVal = str_replace('/',' ',$name);
$retVal = str_replace(' ','-',$retVal);
return $retVal;
}

With corresponding code-


href="'.$site_url.''.safeurl($typ).'/Artist/'.safeurl($artist).'/1/

It replaces spaces with a hyphen, but clicking the link fails!

The site uses a CSV file as data source (no database).

I'm stuck on what to try next?

David

supermanjnk

7:04 pm on Oct 21, 2006 (gmt 0)

10+ Year Member



When you run that code what does your link end up looking like?

seodave

9:03 am on Oct 23, 2006 (gmt 0)

10+ Year Member



Looks like

domain.com/word1-word2/Artist/artist-name/

but doesn't work when clicked!

domain.com/word1%20word2/Artist/artist%20name/

and

domain.com/word1+word2/Artist/artist+name/

Does work in a browser.

David