duhok
duhok

Reputation: 479

Undefined index when using rewriteRule in wamp

I have new project and I am using htaccess to rewrite the URLs. I'm sure there's nothing wrong in my code as I tested it today in xamp and on 000webhost and it worked without any problems.

But when I test the same scripts in wampServer $_GET isn't working on the PHP page, where I get this error:

output is Notice: Undefined index: key in D:\wamp64\www\cp\examples.php on line 5

The problem is with wampServer server. Maybe there is something I should change, but I do not know where the problem lies exactly.

Note: I enabled rewrite_module from wampServer apache module.

htaccess code:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /cp/
RewriteRule ^examples.html$ examples.php //it's working
RewriteRule ^examples/([a-zA-Z0-9]+)?$ examples.php?key=$1

The fifth line of htaccess isn't working with PHP when using:

echo $_GET['key']; //output is Notice: Undefined index: key in D:\wamp64\www\cp\examples.php on line 5

Upvotes: 2

Views: 2481

Answers (1)

Hassan Saeed
Hassan Saeed

Reputation: 7080

add -MultiViews option in your code

What is MultiViews
A MultiViews search is where the server does an implicit filename pattern match, and choose from amongst the results.

For example, if you have a file called configuration.php (or other extension) in root folder and you set up a rule in your htaccess for a virtual folder called configuration/ then you'll have a problem with your rule because the server will choose configuration.php automatically (if MultiViews is enabled,and which is the case most of the time) If you want to disable that behaviour, you simply have to add this in your htaccess Options -MultiViews

Demo Example :

use this code for .htaccess file

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /cp/

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

you examples.php code will be

<?php 
echo $_REQUEST['key'];  
?>

then call http://localhost/cp/examples/27

output will be :27

Upvotes: 4

Related Questions