mod_rewrite url and $_GET

Suppose I have the following URL:

http://www.example.com/view.php?type=cat&name=cuddles

and I mod_rewrite it to:

http://www.example.com/cat/cuddles

Does the new url “http://www.example.com/cat/cuddles” affect the ability of PHP to $_GET[‘type’] and $_GET[‘name’] ?

If so, how would I access the $_GET variables?

Thanks in advance,

No, if you use mod_rewrite $_GET will still be available the same way you’re used to. You don’t need to change anything in the PHP to get it to work with mod_rewrite :slight_smile:

I must have apache configured wrong on my localhost.

The rewrite rule works, but the $_GET variables aren’t being passed to the script.

When I var_dump($_GET), I get array(0).

I’m running Ubuntu 10.10. Are you aware of any common fluke or mistake I must be making? I’ve put about 3 hours into this.

Could you show me the .htaccess ?

I deleted the .htaccess because I was thinking maybe I had multiple .htaccess files competing with eachother.

Even after deleting my local .htaccess file, this rewrite is still working

http://localhost/alpha/view.php?m=mt1230

does

http://localhost/alpha/view/m/mt1230

but the $_GET variable doesn’t load.

multiviews, multiviews, bloody, bloody, multiviews :weee:

Put this in your .htaccess :


Options -MultiViews

1 Like

thanks,
turning off multiviews corrected that problem. (woooh!)

Is there any modrewrite generator that you would suggest as pretty good?

I write it al myself and never use generators, so no, sorry.

thanks for helping me out today. i gotta get better apache skills. thats one of the last areas I’m lacking in.

Hey there,

I’ve had exactly the same problem with a page I’m working on. I wonder if someone could shed some light?

Here’s my index.php script


<html>
<head>
	<title>CMS style mod_rewrite</title>
</head>
<body>
	<p>The requested page was:
		<?php echo $_GET['page']; ?>
	</p>
</body>
</html>

and the .htaccess file:


Options -MulitiViews
RewriteEngine on
RewriteRule ^([^/\\.]+)/?$ index.php?page=$1 [L]

So when I head to index.php/marmite-trousers/, I’m expecting a page that says:
The page requested was: marmite-trousers.

But instead, I’m getting:
The page requested was:
Notice: Undefined index: page in /Users/username/Sites/index.php on line 7

A print_r($_GET); command shows me an empty array.

As you can see I’ve added the -MultiViews option to the .htaccess file, which seemed to work for hypernetware above, but alas I am still marred by a lack of $_GET data.

I’m running apache on mac os x 10.7 (Lion)

Thanks in advance,
Mike

Ok, so the reason is that my .htaccess file is not being read by apache. I’ve submitted another post about this issue, so you can ignore the post above.

Cheers,
Mike

you wouldn’t access the variables using $_GET anymore. You’d use $_SERVER[‘REQUEST_URI’];

for example: http://www.mydomain.com/cat/cuddles

<?PHP
$data = explode(“/”, $_SERVER[‘REQUEST_URI’]);
print_r($data);
?>

this is of course is assuming that .htaccess is working and it done properly.