Page 1 of 1

mod rewrite and relative URLs :(

Posted: Fri Mar 02, 2007 8:12 pm
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:

Posted: Fri Mar 02, 2007 8:28 pm
by feyd
Your rewrite needs tweaking. What exactly, may vary.

Posted: Fri Mar 02, 2007 8:47 pm
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

Posted: Fri Mar 02, 2007 9:17 pm
by feyd
Oh, I'm sorry, I misread your original post. You can use absolute path references e.g. /image1.jpg

Posted: Fri Mar 02, 2007 9:33 pm
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

Posted: Fri Mar 02, 2007 9:41 pm
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" />

Posted: Fri Mar 02, 2007 11:59 pm
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

Posted: Sat Mar 03, 2007 12:07 am
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.

Posted: Mon Mar 12, 2007 3:04 pm
by Dave2000
Hmm. I have concluded that this is impossible - especially if you are dealing with files of a varying number of variables... :(

Posted: Mon Mar 12, 2007 3:32 pm
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*"/>

Posted: Mon Mar 12, 2007 5:09 pm
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;
}
?>

Posted: Tue Mar 13, 2007 12:46 pm
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 :?