使用查询字符串隐藏来自网址的.php扩展名

时间:2015-12-23 05:41:57

标签: php .htaccess url-rewriting

我正在使用.htaccess文件进行网址重写。我用.php扩展写了所有页面并隐藏它们我正在使用这段代码

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

但是当我将“www.example.com/abc.php”写入“www.example.com/abc”时,这隐藏了。我想让它们像点击链接一样自动更改

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

然后页面链接应该像“www.example.com/example”一样打开而不是“www.example.com/example.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>

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

4 个答案:

答案 0 :(得分:1)

使用此.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]

答案 1 :(得分:0)

对于隐藏?val,你应该使用POST而不是GET。并看看on this article代表他在这里定义好的.htaccess。

答案 2 :(得分:0)

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

但是,如果你这样做,你会模糊使用查询字符串的大部分好处。

答案 3 :(得分:0)

要完全删除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]
相关问题