Page 1 of 1
Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 5:22 am
by elchenuk
Hi I am trying to do a simple rewrite:
Code: Select all
RewriteRule ^firstnamesurname$ folder1/page.php?var=Firstname-Surname [NC,L]
What I would like to do then is to take what is in the 'var' of the URL and use it to populate areas of the page. This is the code that I am using on my page
Code: Select all
<?php
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
?>
<?php
$url = curPageURL();
parse_str(parse_url($url, PHP_URL_QUERY), $vars);
$name = $vars['var'];
$customer = str_replace("-"," ",$name);
?>
And then to output the field I am using:
Unfortunately it is not working, the rewrite is working ok but the scripts are not. However when I enter the page URL manually i.e.
http://www.domain.com/folder1/page.php? ... me-Surname
It works perfectly fine.
Can somebody point me in the right direction?
Eddy
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 10:17 am
by x_mutatis_mutandis_x
Which scripts are not working? Is the code you have below from page.php? Can you please clarify..
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 10:29 am
by elchenuk
Sorry if it wasnt clear. The rewrite seems to be working fine as I can view my page however the second snippet of code above doesnt seem to be working. This code is supposed to pull out what is in the var bit of the URL and then populate bits of the web page using the echo command.
I have an update. I added an echo $url within the body of the page to see what the URL looked like and it outputted the request URL (
http://www.domain.com/firstnamesurname) instead of the replacement URL (
http://www.domain.com/folder1/page.php? ... me-Surname) and of course if there is no query string there the code on the page can't do anything?
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 10:49 am
by x_mutatis_mutandis_x
The call to script page.php happens internally, and the $_SERVER['REQUEST_URI'] is not changed to this new URL. You can't have request parameters in the rewrite rule because again, the original URL is what the http connector obtains the request parameters from.
Change your rule to:
RewriteRule ^firstnamesurname$ folder1/page.php [NC,L]
Then use the url:
http://www.domain.com/firstnamesurname? ... me-Surname
should execute folder1/page.php
In page.php use the $_GET global variable to retrieve the value of 'var'
Code: Select all
$name = $_GET['var'];
$customer = str_replace("-"," ",$name);
echo $customer;
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 11:00 am
by elchenuk
Ok cheers for your explanation that makes total sense. So am I limited to giving out a URL with a query string then? I was hoping that I could use a flat URL and this would rewrite to a dynamic URL that I can use etc.
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 12:47 pm
by pickle
So what you want is to give Joe Smith a URL like: domain.com/joesmith/ and have the actual file run be: domain.com/folder1/page.php?var=joe-smith, and therefore generate a page unique to Joe Smith?
Totally do-able, but there are a number of problems with your current approach:
- How would you differentiate between Joe Smith and Joes Mith? You need a dividing character in there.
- You should probably "namespace" these user specific urls so they don't collide with other potential directories.
- Why not store the first & last names in separate variables? For your current purposes you don't really need to, but it doesn't hurt to be future safe.
To fix these problems, start with something like this: domain.com/u/joe/smith/. The /u/ in the URL namespaces these types of addresses & the rewrite rule won't collide with other, legitimate URLs. Your rule would then be:
[syntax]RewriteRule^/u/(.*)/(.*)/$ folder1/page.php?firstname=$1&surname=$2[/syntax]
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 1:07 pm
by elchenuk
Pickle that is exactly what I am trying to do. Problem is that the people I am working with want a URL like domain.com/joesmith/ to make things super simple for their thick clients. But I understand the big issues associated with this.
Following your advice then, can I use the firstname=$1&surname=$2 bit to populate my page?
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 1:48 pm
by pickle
The best you can do is domain.com/joe/smith. You absolutely need the first & surname to be separated - otherwise how will you know where to split them?
The customer isn't always right - if they insist on domain.com/joesmith/ explain to them the impossibilities surrounding that approach.
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 1:56 pm
by elchenuk
Yep agreed. So in your example do I use the same code as I posted in my original post to grab the variables?
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 2:06 pm
by pickle
Both firstname & surname will be in $_GET
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 2:20 pm
by elchenuk
Ok cool so I can do something like:
$firstname = $_GET['firstname'];
$surname= $_GET['surname'];
echo $firstname;
and the fact that my requested URL doesnt have any variables is ok? just reading this from the previous poster
"The call to script page.php happens internally, and the $_SERVER['REQUEST_URI'] is not changed to this new URL. You can't have request parameters in the rewrite rule because again, the original URL is what the http connector obtains the request parameters from."
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 2:28 pm
by pickle
Yes, that's fine. Basically what they were meaning is you can't have domain.com/joe/smith/?age=45, and expect $_GET['age'] to exist.
Re: Need help with getting my rewrite to work
Posted: Thu Apr 19, 2012 2:38 pm
by elchenuk
Brilliant gotcha, thanks so much for taking the time out to help me, if I could send over a virtual beer I would!
Re: Need help with getting my rewrite to work
Posted: Fri Apr 20, 2012 9:36 am
by x_mutatis_mutandis_x
[syntax]RewriteRule^/u/(.*)/(.*)/$ folder1/page.php?firstname=$1&surname=$2 [/syntax]
I'm not sure if you still can access $_GET['firstname'] and $_GET['surname'] in page.php because its a server-side redirect. These are not request parameters passed by the client/browser.
Actually, you don't need a rewrite rule at all. Just have your url like this
http://www.domain.com/folder1/page.php/joe/smith
Then in page.php you can access '/joe/smith' as path info like this
Code: Select all
$name = $_SERVER['PATH_INFO'];
list($x, $firstname, $lastname) = explode('/', $name);
You can refer to the php manual for more details:
http://php.net/manual/en/reserved.variables.server.php