For the life of me I can't seem to figure this out. I still don't understand htaccess rewrite to much.. Ok I have this - RewriteRule ^([^/\.]+)/([^/\.]+)/?$ wow.php?id=$1&bus=$2 When it shows up it has 20% in between it.. - site/14/Blue20%round/ <--- this is what it looks like How would I replace the 20% with a dash - ? site/14/Blue-around/ <--- thats what I am shooting for.
Most probably you need to fix it in PHP and nothing to do in .htaccess file $without_space = str_replace(' ', '-', 'Bluea round'); Code (markup): Use $without_space to create url in php file
I am not sure what I am doing incorrectly but I can't get it to load... before I was getting the title by $item= $_GET['item']; But now I changed that to this \/ and I still can't get it work for some reason. $item = str_replace(' ', '-', '$item'); I have also tried this too - $item = str_replace(' ', '-', 'item'); no such luck... not sure what I am missing here.. I am doing it with pdo I have also tried this - $item = str_replace(" ","-",$_GET['item']);
My advice would be to not use getdata or to even try playing with the URL in the rewriterule. When I'm doing that sort of thing, my rewriteRule just has a whitelist of files to serve without rewrite, and sends EVERYTHING else to my index.php. RewriteRule !\.(gif|jpg|png|css|js|html|htm|txt|ico|zip|gz|rar|pdf|xml|wav|mp3|mp4|mpg|flv|swf|mkv|ogg|avi|webm|woff|svg|eot|ttf)$ index.php Code (markup): Then in my PHP, I explode apart $_SERVER['REQUEST_URI'] by the slashes... after normalizing the slashes (just in case) and trimming off leading and trailing ones. $data = explode('/',trim(str_replace('\\','/',$_SERVER['REQUEST_URI']),'/')); I would wait until AFTER the explode to run urldecode on each value, since the values could contain encoded slashes. A simple foreach by reference handles that nice and quick. foreach ($data as &$d) $d = urldecode($d); That way you're not having to play with $_GET, you're not redirecting legitimate static files, and really you can format the requests however you like. Though that would turn those %20 into what they are SUPPOSED to be, spaces, not hyphens.
I'm wondering if you're going about all of this backwards. Why do you have spaces in the first place? Make sure all links generated replace spaces (characters such as "_" - underscore) with a "-" (dash/hyphen), which can easily be done using what poster #2 mentioned. That's more of a workaround though, the CMS should already be doing this (I'm assuming you're using one). As the URL contains the (product?) ID, you don't need to worry about the rest of the URL (product name) (I assume your CMS isn't so limited and able to use ID alone to look up the product). Of course, you might have URL parameters (e.g. ?page=2&sort=asc), but don't rewrite those for "clean URLs" (they might look prettier, but will cause a headache), just make sure to use canonical URL and you won't end up with duplicate content issues.
I'm sort of building a cms.. How it works is you id & the item which will equal your output.. That way it must have the id and on title. This way it won't be possible to make up a title.. Thats what I am trying accomplish. I will try what he said above and get back to you. Oh yes and to answer your question.. Its because the item has spaces in it. I want people to type whatever they want to type when inserting a item like for example Brown Bananas would outcome as brown-bananas or brown_bananas
Good point about being able to make up a title. If you just use "-" then you won't need to worry about encode/decode. Look at how Drupal does it. Keeps it simple and accurate. Sounds like it would function in the way you want (the URL put in matching up completely, users can put in whatever they want as a title).
Ok I want to thank everyone for there help and wonderful ideas.. I found out what I was doing wrong.. greatlogix you where right.. I was just missing a part.. I am getting the item through the $item= $_GET['item']; $without_space = str_replace(' ', '-', ''.$item.''); Then to double match the records from id and the other \/ <I was missing that section.. WHERE id = '$id' AND title = '$without_space = str_replace(' ', '-', ''.$item.'''" I am not sure if there is a better way of doing this or not..??
Are you doing an SQL look-up without making the input safe first? That would be one critical step missed.
What I use is old. I really need to update it to mysqli. So, I won't paste it, but instead recommend searching for something more recent relating to sanitising input (I just realised that mine won't work with PHP 5.3 and 5.4 without a few tweaks). Anyway, I have a custom function to sanitise input first and then use sprintf to build the query. For your sanitise function, you want one that removes the slashes (such as stripslashes) and then applies mysqli_real_escape_string (but only to non-numeric input). I use sprintf this way (taken from a PHP file I had lying around for an absolutely ancient website): $query = sprintf("INSERT INTO submitted (`Id`, `title`, `yield`, `method`, `ingredients`) VALUES (%s, %s, %s, %s, %s)", $recipe_data['Id'], cleannquote($recipe_data['title']), cleannquote($recipe_data['yield']), cleannquote($recipe_data['method']), cleannquote($recipe_data['ingredients'])); PHP: By the way, I don't know why you don't have a field for the URL, as well as the title, so you don't have to keep to converting it. Just the once when the content is saved (or if the title is changed).