重写php

时间:2016-07-06 10:20:21

标签: php .htaccess

我有一个像

的网址
http://example.com/test.php?pid=105701

我需要删除此网址中的.php。我已经尝试了很多,但网址已经这样了

http://example.com/test/?pid=105701

在上面的url中找到目录结构。无法访问test.php文件

这是我的.htaccess文件

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

帮助我 提前致谢

2 个答案:

答案 0 :(得分:1)

您必须进行一些更正,您还应排除包含.php的所有案例:

RewriteEngine on

# Rewrite filename.php
RewriteRule ^([^.?]+)$ %{REQUEST_URI}.php [L]

# Return 404 if original request is filename.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]

答案 1 :(得分:0)

首先打开apache中的mod_rewrite 那么

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