You are here: Problem with mod_rewrite and replacing spaces in URL « All PHP « IT news, forums, messages
Problem with mod_rewrite and replacing spaces in URL

Posted by David on 10/14/06 03:43

I'm trying to convert a script to use friendly URLs, I've done this
before, but my PHP skills are quite basic so far, far from proficient
at this.

..htaccess file-

DirectoryIndex default.php index.asp index.html index.htm index.php

Options +FollowSymLinks
RewriteEngine on

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

RewriteRule ^([^/]+)/$ default.php?typ=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/$ default.php?typ=$1&page=$2 [L]

RewriteRule ^([^/]+)/([^/]+)/$ default.php?typ=$1&cat=$2 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$
default.php?typ=$1&cat=$2&page=$3 [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/$
default.php?typ=$1&cat=$2&sct=$3 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/$
default.php?typ=$1&cat=$2&sct=$3&page=$4 [L]



Each set of rules creates these URLs-

example.com/$1/Artist/$2/
example.com/$1/Artist/$2/$3/ ($3 is a page number)

example.com/$1/
example.com/$1/$2/ ($2 is a page number)

example.com/$1/$2/
example.com/$1/$2/$3/ ($3 is a page number)

example.com/$1/$2/$3/
example.com/$1/$2/$3/$4/ ($4 is a page number)

It works.

However when variable $2 or $3 (when not a page number), for example
$2 of-

example.com/$1/Artist/$2/
example.com/$1/Artist/$2/$3/ ($3 is a page number)

Includes a space I can't get the PHP script to create an hyphenated
version that works even though at browser level the hyphen is there
(click on it, but the right page does not load).

So a starting URL like

example.com/ringtone/Artist/Britney%20Spears/ (works)

Converted to-

example.com/ringtone/Artist/Britney-Spears/ (doesn't work)

Using a function (see later) that converts spaces to hyphens doesn't
load what I see at example.com/ringtone/Artist/Britney%20Spears/
instead I see a 404 error page (blank script page) and I don't
understand why (I've used the function before)

Using urlencode instead of the function replaces spaces with + so I
see

example.com/ringtone/Artist/Britney+Spears/ (works)

but it's not the format I want and there are other characters I'd like
to remove including ( ) ! &.

So what is wrong with this function (which I don't fully understand
BTW)-



function text2url( $string ){
$string = ltrim($string);
// remove unnecessary spaces and make everything lower case
$string = preg_replace( "/ +/", " ", strtolower($string) );

// special rule for dashes, I think it looks nicer :-p
$string = str_replace(' - ', '-', $string);


// removing a set of reserved characters (rfc2396: ; / ? : @ & = + $
,)
$string =
str_replace(array(';','/','?',':','@','&','=','+','$',',','#'), '',
$string);

// replace some characters to similar ones (more readable uris)
$search = array(' ', 'ä', 'ö', 'ü','ë','ï','é','è','à','ç',);
#$replace = array('_','ae','oe','ue','e','i','e','e','a','c');
$replace = array('-','ae','oe','ue','e','i','e','e','a','c');
$string = str_replace($search, $replace, $string);

// remove everything we didn't so far...
$string = preg_replace("/[^a-z0-9_-]/", "", $string);

// urlencode everything, in case we missed something ;-)
return urlencode($string);
}



When I use it like this

$artist2 = text2url($artist);

echo'<a href="'.$site_url.''.$typ.'/Artist/'.$artist2.'/1/"><img
src="'.$site_url.'button_more_from.gif" alt="All Truetones from
'.$artist.'" border="0"></a>'.$artist.' ';


It converts spaces to hyphens, but within a browser it fails.


This works but adds +

echo'<a
href="'.$site_url.''.$typ.'/Artist/'.urlencode($artist).'/1/"><img
src="'.$site_url.'button_more_from.gif" alt="All Truetones from
'.$artist.'" border="0"></a>'.$artist.'';


Thanks in advance for any help as I'm stumped.

David
--
SEO Tutorial http://www.seo-gold.com/tutorial/
More Earnings Blog http://www.morearnings.com/

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация