Rewrite - Unfriendly URL showing

Discuss practical ways rearrange URLs using mod_rewrite.

Postby RDuno » Thu Dec 11, 2008 7:07 am

Hi, as a newbe i am trying to understand the way URL rewrite works.

I can use .htaccess to rewrite an URL.

But as soon as the visitor is directed to the appropiate URL the actual (unfriendly) URL is shown in his browser.

Conclusion: URL rewrite in .htaccess only translates user-friendly URLs into URLs that work on the server. When the visitor arrives at
the URL (and if he follows other links) he still sees the unfriendly URLs with xxx.php?id=123 etc.

There are several options to change a numeric ID into a name/title. One of them is to use a script file (script.php). This should translate
the name/title and finds the associated ID and then showing the correct page.

I don't know how to do that and if that solves the problem of showing the friendly URL in the browser.

Can anyone help me or link me to a tutorial where this is explained step-by-step ?

It would be highly appreciated.

Thanks,
RDuno
RDuno
 
Posts: 9
Joined: Thu Dec 11, 2008 6:48 am

Postby richardk » Thu Dec 11, 2008 7:46 am

What is your mod_rewrite?

It should be similar to
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^abc/([0-9]+)/?$ /def.php?id=$1 [QSA,L]


When you go to /abc/123/ you will be rewritten to /def.phpid=123 and the URL will (should) not change.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby RDuno » Thu Dec 11, 2008 9:09 am

Hello richardk, :D

You are correct, that is more or less the RewriteRule I use and it
does show in the browser as well. I made a typo earlier and it now works.

BUT, what if a visitor or even more important a search spider walks through the website, he/it will see the unfriendly urls, right ?

I would like it to see the friendly ones only, so:
www.mysite.com/partsdetail/xyz.html
instead of:
www.mysite.com/partsdetail.php?id=4015

I cannot figure out how I can make this possible. I mean, as soon as
the visitor/spider arrives on the website and starts to follow the links,
the unfriendly ones will be shown/get indexed.

I can put friendly URLs into a sitemap and send it to a search machine, but it send it's spider
and probably start following the links from the index.html page. It then will find the unfriendly URLs.

Or am I not correct ?

Thanks,
RDuno :D :D
RDuno
 
Posts: 9
Joined: Thu Dec 11, 2008 6:48 am

Postby richardk » Thu Dec 11, 2008 11:32 am

You have to edit your pages so they output the correct (friendly) links. Mod_rewrite only works on the incoming requests.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby RDuno » Fri Dec 12, 2008 1:02 am

And there is where I get stuck.

I understand that the php page needs to include a script which rewrites the URL by using fields from the database.

I only don't know how to do that.

Can you give me a tip/example ?

Thanks,
RDuno
RDuno
 
Posts: 9
Joined: Thu Dec 11, 2008 6:48 am

Postby richardk » Sat Dec 13, 2008 9:14 am

In your PHP file you will have code that outputs the link, for example
Code: Select all
<a href="/partsdetail.php?id=<?php echo $product_id; ?>">

that you would change to the new style of URL
Code: Select all
<a href="/partsdetail/<?php echo $product_id; ?>.html">
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby RDuno » Mon Dec 15, 2008 1:17 am

Hi Richardk,

Thanks for the hint. I will experiment with this to see if I can get this to work.

I assume you don't have to put the changed URL's in your .htaccess file as well once each page shows the rebuilt URL for visitors and spider/robots, right ?

Again thanks for helping out.

Regards,
RDuno
RDuno
 
Posts: 9
Joined: Thu Dec 11, 2008 6:48 am

Postby richardk » Mon Dec 15, 2008 8:43 am

I assume you don't have to put the changed URL's in your .htaccess file as well once each page shows the rebuilt URL for visitors and spider/robots, right ?

You need the mod_rewrite and to change the links. If you remove the mod_rewrite the new links will not work. Is that what you means?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby RDuno » Mon Dec 15, 2008 9:18 am

Hi,

I have been googling all day today. (I'm located in Europe)

I read that mod_rewrite is necessary, but I still struggle to get the php-code right. I am afraid,
I will have to do more googling :o
RDuno
 
Posts: 9
Joined: Thu Dec 11, 2008 6:48 am

Postby richardk » Mon Dec 15, 2008 9:23 am

Have you found where the links are outputted in the PHP code? Post an example.
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 1 guest

cron