mod rewrite and relative URLs :(

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

mod rewrite and relative URLs :(

Post by Dave2000 »

I am trying to use mod rewrite.

For example, this: example.com/users.php?page=1 redirects to this: example.com/users/1

However, as i would expect, but do not want, this now means all my relative URLs, are appeneded to example.com/users/ making for example, example.com/users/image1.jpg which is not what i want as the image1.jpg is found at example.com/image1.jpg :(

Does this mean i cant use relative URLs with mod rewrite? 8O :cry:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Your rewrite needs tweaking. What exactly, may vary.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

This is what i have at the moment. The number at the end is the only thing that varies (if that's what you meant).
RewriteEngine on
RewriteRule ^user/(.+) user.php?id=$1 [nc]
In my search for an answer i read that [R] may be of help, but i couldn't get it to do what i wanted...

Shears
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Oh, I'm sorry, I misread your original post. You can use absolute path references e.g. /image1.jpg
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

feyd wrote:Oh, I'm sorry, I misread your original post. You can use absolute path references e.g. /image1.jpg
Do you mean, "You can use relative path references" ? - as the example you provided is relative and that was my question...

Anyway. I have another question :lol: How would i do this?

Also, is it possible to make to make redirect rules actually change the link written on the page? For example, /user.php?id=1 redirects to /user/1 , but the link on the page still displays as /user.php?id=1 . Is it possible to change the displayed link on the page using mod rewrite?

Shears
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

A leading slash is absolute pathing. Anything else outside of a fully qualified URL is relative.

An example

Code: Select all

<img src="/image1.jpg" /> vs. <img src="../image1.jpg" /> vs. <img src="image1.jpg" />
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

feyd wrote:A leading slash is absolute pathing. Anything else outside of a fully qualified URL is relative.

An example

Code: Select all

<img src="/image1.jpg" /> vs. <img src="../image1.jpg" /> vs. <img src="image1.jpg" />
Ah thank you :) I didn't know that. It helps alot. I have admitted the leading slash on all my links on all pages lol... /me sighs... i guess i have alot of work ahead of me converting them all :(

Now about links. :lol: Obviously, where there is a link shown to the user, i would prefer that it is displayed as user/1 . Do you think i should write the links as /user/1 in my code ( rather than /user.php?id=1 )? ...or, is there a way to make redirect rules actually change the link written on the page? ...and if so, is there much overhead in doing this?

Thank you

Shears
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I would suggest writing some code, a function, to generate the url for you. That way a simple toggle or change can switch from "/user/1" to "/user.php?id=1" .. most of your URL's would need to go through this function.
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

Hmm. I have concluded that this is impossible - especially if you are dealing with files of a varying number of variables... :(
nickvd
DevNet Resident
Posts: 1027
Joined: Thu Mar 10, 2005 5:27 pm
Location: Southern Ontario
Contact:

Post by nickvd »

If you are using clean url's and you want to use relative paths in your site, then use the <base/> tag at the top of your <head>

Code: Select all

<base href="*baseurl*"/>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Shears wrote:Hmm. I have concluded that this is impossible - especially if you are dealing with files of a varying number of variables... :(
It is quite possible, but you need to start clean. I use a single function that I literally pass every link through so it gets written the way my config tells it to be. This is, of course only a glimpse of the logic, but you get the idea from this...

Code: Select all

<?php
function rewrite_href($url)
{
  if (empty($url))
  {
    return false;
  }

  if ($this->settings->get('config.rewrite_clean_urls'))
  {
    // handle rewiting a clean url
  }
  else
  {
    // No rewriting
  }

  return $url;
}
?>
Dave2000
Forum Contributor
Posts: 126
Joined: Wed Jun 21, 2006 1:48 pm

Post by Dave2000 »

My best attempt is...

Code: Select all

function rewrite_url($file, $array = false) 
{
	$url = preg_replace('/(\w)\.php/i', '$1', $file) . '/';
	
	if ( $array != false ) 
	{
		foreach ( $array as $value => $key )
		{
			$url .= $value . '/' . $key . '/';
		}
	}
	return $url;
}

rewrite_url('users.php', $array = array('u' => '1'));
which well, seems a bit long and lame to me, and i'm sure there will probably be times when it wont work :?
Post Reply