Leona M. Branham Leona M. Branham - 3 months ago 7
PHP Question

How to make http://example.com/test.php to http://example.com/index.php?do=test using .htaccess?

I have a default url

http://example.com/test.php


I want to make this url to
http://example.com/index.php?do=test





http://example.com/test.php
to
http://example.com/index.php?do=test


I am using below code, but not working...



RewriteEngine On
RewriteCond %{REQUEST_URI} !^index\.php$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(\w+)\.php$ index.php?do=$1 [L]


Thanks in advance.

Answer

Use following

.htaccess file

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

index.php

<?php 
if(isset($_GET['do']) && !empty($_GET['do'])){
    include $_GET['do'] . '.php';
} else {
    include 'index.php';
}
?>

test.php

<!DOCTYPE html>
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    Hello You are in Test Page
</body>
</html>

URL : http://localhost/index.php?do=test

Comments