'How to remove index.php and index from the URL using htaccess | PHP

I am working on a PHP website for QA forum. My website contains a qa folder like -

example.com/qa/ In the qa folder, I have an index.php file. I am getting QA post from datatabase like -

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

I am using htaccess code (htaccess located in the same qa folder ) -

RewriteEngine on

    RewriteRule ^index/([0-9]+)/([0-9a-zA-Z_-]+)$ index.php?id=$1&slug=$2 [NC,L]

According to this htaccess file, I can open URL like -

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

not

example.com/qa/index.php?id=506&slug=how-to-connect-the-html-page-to-the-mysql-database-using-php

This is working well and I like it but the main thing starts from here.
I want to remove index from url

before

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

after

example/qa/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

I have used this code in the same htaccess file

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]

RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

but it's not working.
One more thing, I will create more pages in qa folder like - login.php, register.php.

How can I remove the index from the URL without effects on other pages in qa folder.



Solution 1:[1]

How to remove the index from url

example.com/qa/index.php?id=506&slug=how-to-connect-the-html-page-to-the-mysql-database-using-php

to

example/qa/index/506/how-to-connect-the-html-page-to-the-mysql-database-using-php

before

Just create a htaccess file in your folder where is index.php located.

RewriteEngine on
RewriteRule ^([0-9]+)/([0-9a-zA-Z_-]+)$ index.php?id=$1&slug=$2 [NC,L]

after

example/qa/506/how-to-connect-the-html-page-to-the-mysql-database-using-php 

It will be more valuable for future visitors. If you want to remove PHP (.php ) extension from any page inside the folder

before

example.com/qa/login.php 

after Add code line on htaccess file

 RewriteEngine On
    RewriteRule ^login\/?$ login.php

Now, you can open URL like -

example.com/qa/login

It's another htaccess method to remove PHP extension .php from the Page URL using htaccess file .

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1