coop coop - 3 months ago 14
PHP Question

$_GET empty when using .htaccess rewrite rule

I am trying to get the var after the page name (test2), but it is empty in php.

url: https://example.com/test2/34

.htaccess:

Options +FollowSymlinks
RewriteEngine on

DirectoryIndex index.php

RewriteRule ^test2 index.php?page=test2&id=$1 [QSA]


php $_GET vardump

array(2) { ["page"]=> string(5) "test2" ["id"]=> string(0) "" }

Answer

It is because you are not matching and capturing value after test2/.

You can use:

DirectoryIndex index.php
Options +FollowSymlinks

RewriteEngine on    

RewriteRule ^test2/(.*)$ index.php?page=test2&id=$1 [QSA,L,NC]