How to Remove index.php from URL in PHP

2019-07-19 02:54发布

问题:

I want to remove index.php from url but so far i couldn't succeed it. I'm using Wamp server on my local and Apache on remote server.. In local root directory, my project files are located in a subfolder like

www/project/index.php

I can access web pages like

localhost/project/index.php/home

localhost/project/index.php/messages/?mId=3

I just want to access it like

localhost/project/home
localhost/project/messages/?mId=3

I already tried some .htaccess rewrite rule but couldnt make it.

回答1:

Here's how you need to organize:

<ifModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>

And then you will have everything as you need.



回答2:

You will want to use url rewriting with a .htaccess file:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Put a .htaccess with this content in each subfolder.