Jump to content

$_GET not working - possibly rewriterule in htaccess?


ashburnham

Recommended Posts

Hi there,

 

I have a script.php file that generates all the html pages on my site but it is unable to pick up on items in the url.

 

The set up is a .htaccess file saying:

 

Options +FollowSymlinks
rewriteengine on
rewriterule ^(.+)\.html$ /script.php?url=$1.php
rewriterule ^(.+)\.htm$ /script.php?url=$1.php

 

and a script.php saying:

 

$url = str_replace("script.php?url=", "", $url);

if ($url==test1){
// display some html
}

if ($url==test2){
// display some other html
}

 

All this means if you enter a url of www.example.com/test1.htm, the script.php is accessed and finds where $url=test1 and the appropriate html output is shown.

 

My problem is where a url is entered of www.example.com/test1.htm?name=bob.  The page is displayed correctly still and the url at the top of the page still shows www.example.com/test1.htm?name=bob but my script.php cannot seem to find or handle the name=bob part.

 

I'm using $name = $_GET["name"] in my script.php but nothing.  I shouldn't even have to do this as my php.ini has the option switched on to automatically assign items in the url as a variable.

 

Now I am beginning to think it is possibly the rewriterule in the htaccess file?  Is there anyway to sort this out so I can use items from the url?

 

Thanks

I'm not too familiar with rewriterules, but it would seem to me that if you used the htm or html syntax with a get appended to the end, the regex in the rewriterule wouldn't match anything. You are matching .html to the end of a string and if you put ?name=bob on the end, it is no longer html. You don't give enough information for me to say that that is the problem or not, but I would look into that as being an issue. In any case, even if it DOES match that line with the get variable at the end, when it changes the url, it completely ignores anything else in the original url. There isn't really a way to get around that when using the rewrite unless you only expect a certain number of variables in get and you can include that in the regex.

This is really beyond me (I'm picking up on something very unfamiliar from the person who originally wrote the code before me).

 

It is a case of the "name=bob" part is on 1 out of 10 urls so cannot write it for one way or the other but my knowledge is very limited so don't really know what else I can tell you.

 

Thanks anyway.

You can probably add another rule like this:

Options +FollowSymlinks
rewriteengine on
rewriterule ^(.+)\.html$ /script.php?url=$1.php
rewriterule ^(.+)\.htm$ /script.php?url=$1.php
rewriterule ^(.+)\.htm\?name=(.+)$ /script.php?url=$1.php&name=$2

 

You might want to mess with that regex, I think there might be a problem with using a greedy plus, but I'm not sure.

 

After you add an extra rule like that, you should be able to do something like this inside your script.php file:

if (isset($_GET['name']))
   //do stuff

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.