Page 1 of 1

[SOLVED] Removing slashes and spaces in URL

Posted: Wed Aug 04, 2004 1:06 am
by ghank
Been trying to figure this one out for a day with little luck so here it is. Yahoo decided to drop the ending slashes on my directory style links in their search engine. For example, a Yahoo search shows my url as domain.com/az when it should be domain.com/az/. When you click you get a 404 error. I am using this rewrite in my htaccess file to set a url variable:

Code: Select all

RewriteEngine On

Rewritecond %{http_host} ^www.*$ 
RewriteRule (.*) http://domain.com/$1 їR=301,L]
RewriteCond %{REQUEST_URI} !-s
RewriteRule ^az/(.*) /template/aztemplate.php?dkey=$1
So then my template searches a mysql db for a matching URL to create a page. But without the slash, nothing is found. Here is my testing code:

Code: Select all

<?php

//db connection removed for simplicity
$dkey = (string) $_GET['dkey'];
echo $dkey;
?>
The URL domain.com/az/ returns az/ and domain.com/az returns az without the slashes. I also have URLs that look like domain.com/az/city/. I am pretty new to PHP but I think I need to strip the slashes and spaces, put them in an array since there will be one or two directories, and put the slashes back, but I am stumped on how to do that. Any help would be so appreciated as my brain is fried.

Posted: Wed Aug 04, 2004 3:13 am
by JAM
This is perhaps not the best example, but it might get you more ideas:

Code: Select all

$test[] = 'http://www.example.com/foo/';
    $test[] = 'http://www.example.com/foo';
    $test[] = 'http://www.example.com/foo/bar/';
    $test[] = 'http://www.example.com/foo/bar';

    foreach ($test as $key => $val) {
        $tmp = pathinfo($val);
        echo $tmp['dirname'].'/'.$tmp['basename'].'/';
    }
You could also use strstr() or substr() functions to verifiy if the last char is a /, substr_count() to count char's, explode() to break it into an array and so on.

Posted: Wed Aug 04, 2004 6:47 pm
by ghank
Thanks for the tip! I couldn't figure out the syntax for adding the slash to the variable but your example helped me out. Man, I still need to get a php book to help because I suck at this stuff.