homepage Welcome to WebmasterWorld Guest from 54.205.106.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite keeps repeating pattern
when clicking links
Walley




msg:3904637
 2:20 am on May 1, 2009 (gmt 0)

I've been trying to figure this out forever but can't seem to find anything. I'm using XAMPP.

This is .htaccess file

[b]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^backgroundscategory/(.*)$ backgroundscategory.php?category=$1 [NC,L]
</IfModule>
[/b]

This is the php page with the link change

[b]
<?php
$categoryquery = 'SELECT * FROM backgroundcategory ORDER BY category ASC';
if ($categoryresult = mysql_query ($categoryquery)) {
while ($categoryrow = mysql_fetch_array ($categoryresult)) {
$categorylink = $categoryrow['category'];
echo "<a href=\"backgroundscategory/$categorylink\" class=\"backgroundsfg\">$categorylink</a><br />";
}
}
?>
[/b]

The problem is when the link is click it keeps adding backgroundscategory/ to the url... so if I keep clicking the links from that while loop the url will end up looking something like this:

[b]
http://example.com/backgroundscategory/backgroundscategory/backgroundscategory/backgroundscategory/category
[/b]

[edited by: Walley at 2:24 am (utc) on May 1, 2009]

[edited by: jdMorgan at 2:53 am (utc) on May 1, 2009]
[edit reason] example.com [/edit]

 

jdMorgan




msg:3904647
 2:52 am on May 1, 2009 (gmt 0)

It is the client (e.g. browser or search engine robot) that resolves relative links on the pages it displays in order to produce the canonical URLs that it requests when you click on one of those links.

Your php is publishing a page-relative link. The browser looking at the page already thinks that it is looking at a page at example.com/backgroundcategory/something (look at the browser address bar to confirm). So, when it sees a relative link of <href="backgroundcategory/somethingelse"> on that page and you click on that link, the browser strips off the "currently-being-viewed-page path" ("something") from the location showing in its address bar, and then adds the contents of the relative link, resulting in a requested URL of /backgroundcategory/backgroundcategory/somethingelse

Each time you click another relative link on a page previously requested by a relative link, you end up going another "/backgroundcategory" level deeper.

The usual solution is to use a server-relative link, instead of a page-relative link:

echo "<a href=\"/backgroundscategory/$categorylink\" class=\"backgroundsfg\">$categorylink</a><br />";

When you do, this, the leading slash tells the browser to discard everything except the protocol and domain in its address bar, and then add the server-relative path to that in order to resolve the canonical URL.

(You could also echo-out a full, canonical URL of "http://example.com/backgroundcategory/$categorylink" if you wanted to. In that case, the browser replaces the entire URL in its address bar.)

See if that helps...

Jim

Walley




msg:3904687
 4:27 am on May 1, 2009 (gmt 0)

Thanks for all the information.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved