Search friendly urls

Discuss practical ways rearrange URLs using mod_rewrite.

Search friendly urls

Postby Gert » Mon Jul 20, 2009 8:54 pm

Hi,

I’ve been browsing through the posts in the SEF group but can’t find what I’m looking for. If my question was already answered somewhere than maybe somebody can direct me to it.
My question is:

I would like to rewrite url’s like
/trips/Africa/Kenia/Title-of-the-trip
to
/tripsdir/showtrip.php?tripcode=KET
without changing the url.

In a database I’ve got the following vars:
continent
country
title
tripcode

I can write a .htaccess that would redirect the SEFs to the right page but not without changing the url.

Do I have to include below code in .htaccess to have all requests for directories and files which can’t be found sent to index.php? And if so, what should be in index.php?

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

Thanks,
Gert
Gert
 
Posts: 10
Joined: Sun Jul 19, 2009 6:26 pm

Postby richardk » Tue Jul 21, 2009 11:56 am

Do I have to include below code in .htaccess to have all requests for directories and files which can’t be found sent to index.php?

You don't need that. You just needs /trip URLs to be sent to a file, probably not index.php. It Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^trips/([-a-z]+)/([-a-z]+)/([-a-z0-9]+)/?$ /tripsdir/showtrip.php?continent=$1&country=$2&title=$3 [QSA,L]

in a .htaccess file in your document root.

And if so, what should be in index.php?

Then you could edit /tripsdir/showtrip.php to use the continent, country and title instead of the tripcode $_GET variable. Or you could create a different file to do it.

It's similar to FAQ: How to change a numeric ID into a name/title (where the numeric ID is the tripcode and the name/title is the other variables).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gert » Tue Jul 21, 2009 3:07 pm

Thanks Richard.

I've tried your solutions with the redirect script but I still end up with the URL
/trips/showtrip.php?rc=tripcode

while I would like

/trips/Africa/Kenia/Title-of-trip/

to be shown as the URL address and only have the showtrip.php script loaded in the background.

I'm working now with the php header() statement to redirect
/trips/Africa/Kenia/Title-of-trip/
to
/trips/showtrip.php?rc=tripcode

Maybe that's not the right command but I don't know of another way.

Do you have a suggestion?
Thanks,
Gert
Gert
 
Posts: 10
Joined: Sun Jul 19, 2009 6:26 pm

Postby richardk » Wed Jul 22, 2009 11:21 am

I've tried your solutions with the redirect script but I still end up with the URL
/trips/showtrip.php?rc=tripcode

Mod_rewrite does not change the links in the HTML source. You have to edit your scirpt to output the new links.

If you put
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^trips/([-a-z]+)/([-a-z]+)/([-a-z0-9]+)/?$ /tripsdir/showtrip.php?continent=$1&country=$2&title=$3 [QSA,L]

in your .htaccess file and go to /trips/Africa/Kenia/Title-of-trip/ what happens?

Also, because tripcode is not in the new URL, you have to edit your script to use the other variables instead. Or you could add tripcode to the new URL.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gert » Wed Jul 22, 2009 1:47 pm

If I go to /trips/Africa/Kenia/Title-of-trip/ I'm redirected to /tripsdir/showtrip.php?rc=tripcode (of the designated trip). So the URL changes and that's something I don't want.

In .htaccess I've got:

Options +FollowSymLinks
IndexIgnore */*

RewriteEngine On

RewriteRule ^trips/([-A-z]+)/([-A-z]+)/([-A-z0-9]+)/$ /redirect.php?continent=$1&country=$2&title=$3 [QSA,L]

I have edited the scripts to use the variables as you can see here:
(redirect.php)
-------
$land=$_REQUEST["country"];
$regio=$_REQUEST["continent"];
$titel=str_replace("-"," ",$_REQUEST["title"]);

$result = mysql_query("SELECT * FROM reisinfo WHERE regio='$regio' AND land LIKE '%$land%' AND titel LIKE '%$titel%'",$db) or die ('I cannot SELECT from the database because: ' . mysql_error());
$myrow = mysql_fetch_array($result);
$tripcode=$myrow["rc"];

$location = "/tripsdir/showtrip.php?rc=".strtoupper($tripcode);
header( "Location:".$location );
---------

That's like you suggested I thought. But to no avail. Any further thoughts?
Thanks.
Gert
 
Posts: 10
Joined: Sun Jul 19, 2009 6:26 pm

Postby richardk » Wed Jul 22, 2009 3:53 pm

Don't redirect, include()
Code: Select all
// Escape the variables.
$land  = mysql_real_escape_string($_REQUEST['country'], $db);
$regio = mysql_real_escape_string($_REQUEST['continent'], $db);
$titel = mysql_real_escape_string(str_replace('-', ' ', $_REQUEST['title']), $db);

$result = mysql_query("SELECT * FROM reisinfo WHERE regio='$regio' AND land LIKE '%$land%' AND titel LIKE '%$titel%'",$db)
  or die ('I cannot SELECT from the database because: ' . mysql_error());
$myrow = mysql_fetch_array($result);

// Set the variables /tripsdir/showtrip.php needs.
$_GET['rc'] = $_REQUEST['rc'] = strtoupper($myrow["rc"]);
// Get rid of the other variables.
unset($_REQUEST['country'], $_REQUEST['continent'], $_REQUEST['title'], $land, $regio, $titel, $result, $myrow);
include('./tripsdir/showtrip.php');
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gert » Wed Jul 22, 2009 8:48 pm

Yes !!, eureka, it's working.

Thanks a lot Richard.

The only issue I have now is how to refer to images, css and js in the showtrip.php script. Relative addressing like '../images/imagename.gif' or '/images/imagename.gif' isn't working anymore. I have to use 'http://www.sitename.com/images/imagename.gif' to have them displayed.

It probably has to do with the url being:
http://www.sitename.com/trips/Africa/Ke ... -the-trip/

Any idea how I can solve this?
Thanks
Gert
 
Posts: 10
Joined: Sun Jul 19, 2009 6:26 pm

Postby richardk » Thu Jul 23, 2009 9:07 am

The only issue I have now is how to refer to images, css and js in the showtrip.php script. Relative addressing like '../images/imagename.gif' or '/images/imagename.gif' isn't working anymore. I have to use 'http://www.sitename.com/images/imagename.gif' to have them displayed.

FAQ: Relative paths to images, JavaScript, CSS and other external/linked files are broken.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby Gert » Thu Jul 23, 2009 4:47 pm

It doesn't make sense. For some links to scripts I have to use relative addressing like '../" and for others '/'. For the images I have to include 'http://www.domain.com/'.
Tried <base href but that doesn't work.
Very strange.
Gert
 
Posts: 10
Joined: Sun Jul 19, 2009 6:26 pm

Postby richardk » Fri Jul 24, 2009 12:47 pm

Can you provide some examples, including the page URL (without domain if you don't want to post it) and the location of the file on the server.

Tried <base href but that doesn't work.

What have you set the <base> to? Try setting it to
Code: Select all
<base href="http://www.example.com/">

and then setting the paths relative to it (eg. "images/image.png" for http://www.example.com/images/image.png). Although, if you have to change all the paths, making them "absolute", eg. "/images/image.png" should work.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 21 guests

cron