views:

2777

answers:

5

I've the following rewrite rule in .htaccess:

RewriteRule ^groups/([^/\.]+)/?$  groupdetail.php?gname=$1 [L,NC]

This takes something like www.example.com/groups/groupname and calls www.example/groupdetail.php?gname=groupname. And it works just fine.

But all the relative links on groupdetail.php use groups/ as the relative path, and I don't want them to. How do I avoid this?

For example, when a user clicks on a link <a href="link.php"> on groupdetail.php?gname=groupname, he's taken to www.example/groups/link.php. I want to take the user to www.example.com/link.php.

Obviously, I want to URL to the user to look like "www.example.com/groups/groupname" so I don't want to use [R]/redirect.

A: 

If you change the rewite rule to do a force redirect (add the [R] option), then the browser will be using the /groupdetail.php URL and the relative links will work fine. However, that adds one redirect and makes the URLs less pretty.

RewriteRule ^groups/([^/.]+)/?$ groupdetail.php?gname=$1 [L,NC,R]
Kevin Hakanson
Kevin, yes that would work but I want to URL to the user to look like "www.example.com/groups/groupname" so I don't want to use [R]/redirect.
A: 

Relative links are resolved by the browser, not the server, so there is nothing you can do with mod_rewrite.

Either use relative links the go up the hierarchy (../link.php) or use absolute links.

hop
so, could somebody please enlighten me, as to how this answer is wrong or bad?
hop
hop, I agree that your comment does not deserver the -4 it got, I've given you an upvote.
Toby Allen
strange things happen to this answer... now it's suddenly -1 again from only one upvote? ...anyway, maybe i'm wrong, but if so, i'd like to know why!
hop
+1  A: 

If like me you had hundreds of relative links in the page, insert a <base href=""> in the <head> with an absolute path (could use relative too). You'll need to also make the path to .js files in the <head> absolute because IE and firefox deal with the base href differently. I agree it is an annoying issue.

A: 

Hop's answer is correct. The browser sees www.example.com/groups/groupname as the address, so considers that /groups is the current directory. So, any links like <a href=link.php> are assumed to be in the /groups folder.

When the user moves his mouse over the link, he'll see www.example.com/groups/link.php as the link address.

The solution is to use absolute links -- just add a slash before the href:

<a href=/link.php>

The user will then see www.example.com/link.php as the url.

That said, it seems from your question that you are using relative links on purpose... do you have a reason not to use absolute links?

Andrew Swift
A: 

You can use the BASE tag, if you don't want to use absolute paths:

http://www.w3schools.com/tags/tag%5Fbase.asp

Razvan