Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does $_GET = index.php

In my .htaccess file I have:

RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+)/?$ index.php?ToDo=$1 [L,QSA]

When doing a print_r($_GET), it outputs

Array ( [ToDo] => index.php ) 

however, when I change the Rewrite Rule to

^([^/.]+)/?$ index.php?ToDo=$1 [L,QSA] 

print_r then outputs that $_GET is an empty array. Can someone explain to me why this occurs?

like image 770
Matt Himsl Avatar asked May 13 '26 02:05

Matt Himsl


1 Answers

Let's take an example URI /abc:

Your first regex:

^([^/]+)/?$

Matches /abc and rewrites it to:

/index.php?ToDo=abc

Now mod_rewrite engine runs again and matches the regex ^([^/]+)/?$ again for URI /index.php and rewrites it to:

/index.php?ToDo=index.php

Your 2nd regex:

^([^/.]+)/?$

works fine because it doesn't match /index.php URI.

Best way to write this rule is like this:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?ToDo=$1 [L,QSA]

These two RewriteCond lines will prevent rewriting if request is for a valid file or directory.

like image 190
anubhava Avatar answered May 15 '26 16:05

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!