htaccess remove .php and keep query string

2020-02-29 06:23发布

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

3条回答
时光不老,我们不散
2楼-- · 2020-02-29 06:29

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
查看更多
We Are One
3楼-- · 2020-02-29 06:34

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
查看更多
一纸荒年 Trace。
4楼-- · 2020-02-29 06:48

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]
查看更多
登录 后发表回答