[SOLVED] Removing slashes and spaces in URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

[SOLVED] Removing slashes and spaces in URL

Post 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.
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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.
ghank
Forum Commoner
Posts: 35
Joined: Tue Apr 06, 2004 3:21 pm

Post 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.
Post Reply