perqedelius perqedelius - 2 months ago 7
PHP Question

htaccess redirect URL with sub directories to php file

I need to redirect a Specific root URL (in this example "test") with unknown sub directories to a php file.

example URL

http://domain.com/test
http://domain.com/test/sub1
http://domain.com/test/sub1/sub2


I have tried with this .htacces code

RewriteRule ^test(/.*)?$ redirect.php?path=$1


"Edit"

I use this code to split the $path

$path = explode("/", $_GET['path']);


But when I run print_r on $path in my redirect.php file array 0 is empty

Array ( [0] => [1] => sub1 [2] => sub2 )


How can I get the full path in my array?

Answer

If you want to capture full path then use RewriteCond with REQUEST_URI variable:

RewriteCond %{REQUEST_URI} ^/(test(?:/.*)?)$ [NC]
RewriteRule ^ redirect.php?path=%1 [L,QSA]
Comments