Htaccess隐藏文件扩展名并保留$ _GET参数为Access

时间:2019-05-28 05:35:25

标签: php .htaccess mod-rewrite

首先我有2个文件,分别是index.php和main.php,在这两个文件中,我已经包含了$ _GET参数,因此可以使用此参数进行访问

index.php?page=login
index.php?page=register&ref=1 
main.php?page=member
main.php?page=member&edit
main.php?page=logout

通过使用.htaccess,我想删除.php扩展名,但我想保留该参数并使用新的url访问它

我编写了这段代码

RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php?\?p=([^\s]+)? [NC]
RewriteRule ^ /%1? [R=302,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php?\?p=([^\s]+)&[A-Z]=([^\s]+) [NC]
RewriteRule ^ /%1?/%2? [R=302,L,NE]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p=$1  [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ main.php?p=$1  [L,QSA]

是的,我只能使用http://ex.com/index和main.php http://ex.com/main访问index.php,但请看一下这段代码

RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?p=$1  [L,QSA]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ main.php?p=$1  [L,QSA]

当“ index.php”规则位于顶部时,我可以访问http://ex.com/loginhttp://ex.com/register(index.php中的参数),但不能访问http://ex.com/member或{{ 3}}(main.php中的参数)

我想要的是通过使用参数来访问所有文件。 谢谢

1 个答案:

答案 0 :(得分:0)

这是要使用的代码

RewriteRule ^register/?$ index.php?p=register  [L]
RewriteRule ^login/?$ index.php?p=login  [L]
RewriteRule ^member/?$ main.php?p=member  [L]
RewriteRule ^logout/?$ main.php?p=logout  [L]

但是可以,该参数可以通过某种方式访问​​。只是想了解更多。