htaccess remove .php and keep query string

强颜欢笑 提交于 2019-12-18 17:36:06

问题


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


来源:https://stackoverflow.com/questions/15507132/htaccess-remove-php-and-keep-query-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!