Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite query_string problem

Using mod_rewrite to manipulate urls and access URL parameters

         

raytube

7:13 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



Hi there,
I'm pretty new to the world of mod_rewrite but after spending hours (days?) searching for info I have come up with a mod_rewrite script that is intended to change a dynamic URL into a static one. Just to make it clear, the intention is to use URLs that are search engine friendly. I also want to access the url parameters for use in php script on the page.

My .htaccess file is as follows:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
Rewritecond %{query_string} ^id=/(.*)$
RewriteRule ^getloc.php$ /getloc/%1? [L]

My static URL is:
[localhost...]

The underlying dynamic URL is:
[localhost...]

I'm sure there's something simple wrong with it, I have a script in my php page to access and output the 'id' url parameter but it doesn't work when using the static URL, but it does when using the dynamic form.

Does anyone have any ideas? I'm afraid my brain is in information overload due to all of the different threads I've read about this and I just can't see what's wrong.

Thanks a lot for any tips!

Rick

sabai

7:24 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



Yes, just a little mistake, remove the forward slash from the rewritecond line...

Rewritecond %{query_string} ^id=(.*)$

sabai

7:27 pm on Mar 10, 2004 (gmt 0)

10+ Year Member



actually it seems to work with the forward slash too - probably the mistake is in your script detecting the ID.

raytube

9:53 am on Mar 11, 2004 (gmt 0)

10+ Year Member



Thanks Sabai,
I appreciate your response : ) So, if that Rewritecond etc is ok then it's basically the method used to grab the url parameters. This is basically the main problem I've been having, I had looked through a few examples of what I'm trying to do and finally came across the Rewritecond part. If I understand correctly this should make the ID url parameter available to my script, is this correct?

I'm using PHP to grab the url parameters, two different methods are used and they both work when I just use the standard URL like:

[localhost...]

but not when it is the static URL like:

[localhost...]

My PHP script is:

<?php $locvar = $_GET['id']; echo "Location is $locvar";?><br />
<?php echo $_SERVER['REQUEST_URI'];?><br />
<?php echo $HTTP_GET_VARS["id"];?>

Am I missing something or am I understanding correctly that with the Rewritecond statement in my .htaccess file that I should now be able to access these parameters using the static URL?

After reading my initial post I just wanted to confirm my intention, which is to have static urls (2004/getloc/10) map to (2004/getloc.php?id=10) therefore having search engine friendly URLs.

Once again I really appreciate any help offered :)

Rick

sabai

11:40 am on Mar 11, 2004 (gmt 0)

10+ Year Member



since the URL has been rewritten, you can't access the ID as a GET variable since it isn't there any more... you'll have to parse it from $_SERVER['REQUEST_URI'].

Splosh

4:19 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



The way i understand mod_rewrite is that you enter into .htaccess something like

RewriteEngine on
RewriteBase /
RewriteRule ^info/(.*).html$ /info.php?phrase=$1 [L]

This should, as i understand it, rewrite

www.mydomain.com/info/foo.html

to

www.mydomain.com/info.php?phrase=foo

Therefore, passing foo to info.php as variable phrase

This should then be available for php to use.

The problem I am having is that on my testing server, this works fine, no problems at all. But when i upload the code to my clients server, it doesnt.

Any ideas what could be causing this?

Could it be something to do with Global Variables being on/off?

Is there a workround that doesnt involve parsing?

Any advise would be appreciated.

sabai

7:48 pm on Mar 11, 2004 (gmt 0)

10+ Year Member



raytube - I'm sorry, my last post was nonsense... of course you can still access the get variable.

Splosh - I think you need a backslash to escape the . before the html - (.*)\.html

closed

4:58 am on Mar 12, 2004 (gmt 0)

10+ Year Member



Splosh:
Any ideas what could be causing this?

You haven't been very specific about where the problem is, so it's hard to tell. It could be that the server you upload to is not an Apache server, or doesn't have mod_rewrite, or it could be a problem with PHP. So you'd have to check your log files for any problems with the server, then check how PHP is set up on the server if PHP is the problem. Or it could be something else?