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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With