htaccess remove .php and keep query string

2020-02-29 06:29发布

问题:

Here is my .htaccess file right now.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]

This works in the fact that it makes my pages accessible when not using the .php extension.

Old = domain.com/test.php
New = domain.com/test

The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?

domain.com/test?id=1

回答1:

Matching the entire query string and appending it to your new URL using a back-reference should work.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]


回答2:

If you are passing id then you have to use this code

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1

now you can access

domain.com/test?id=1

from

domain.com/test/1


回答3:

Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php