使用查询字符串从url中隐藏.php扩展名


Hide .php extention from urls with query string

我正在使用.htaccess文件进行url重写。我已经用.php扩展名编写了所有页面,为了隐藏它们,我使用了以下代码

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^'.]+)$ $1.php [NC,L]`

但当我把"www.example.com/abc.php"写为"www.example.com/sabc"时,这会隐藏起来。我希望它们能像点击链接一样自动更改

<a href="example.php">example</a>

那么页面链接应该像"www.example.com/example"而不是"www.example.com/sample.php"那样打开

此外,如何从所有页面隐藏"?var="hello",但变量应该发送到该页面,这是可能的吗?.

更新

例如,我在这里使用这个

<?php 
if(isset($_GET['hi'])){
echo $_GET['hi'];
}
?>
<a href="a.php?hi=sdsdsd">a</a><br>
<a href="b.php">b</a><br>
<a href="c.php">c</a><br>
<a href="d.php">d</a><br>

我希望页面应该在没有扩展的情况下访问,查询字符串应该隐藏,但必须访问。

使用this.htaccess:

RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)'.php([#?][^' ]*)?' HTTP/
RewriteRule ^(.+)'.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

隐藏?val您应该使用POST而不是GET。看看这篇文章。他在这里定义了好的访问。

关于隐藏查询字符串。当php页面运行时,它可以首先检查查询字符串。如果找到了,您可以从查询字符串中提取数据,将其存储在某个位置(可能是会话),然后重定向到没有查询字符串(也没有文件扩展名)的页面。

但是,如果你这样做,你就掩盖了使用查询字符串的大部分好处。

要完全删除php扩展,可以使用:

 RewriteEngine on
#1redirect "/file.php" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)'.php [NC]
RewriteRule ^ %1 [NC,L,R]
#if the request is not for a dir
RewriteCond %{REQUEST_FILENAME} !-d
 #and the request is an existing php file
 RewriteCond %{REQUEST_FILENAME} !-f
 #then rewrite the request to orignal php file
 RewriteRule ^([^/]+)/?$ $1.php [NC,L]