带有$ _GET方法的漂亮网址

时间:2019-08-16 21:32:59

标签: php .htaccess

我是编码的新手,所以非常感谢您提供其他解释。到目前为止,我已经能够使用htaccess文件删除文件扩展名。

Options -MultiViews
RewriteEngine on

# Does not apply to existing directories, if directory exist then don't run the rule

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php [NC,L]

在“仪表板”目录中,我还有另一个index.php(仅显示给登录的用户)。在索引中,我有一个$ _GET方法来检索用户的状态。因此,当我访问localhost / project / dashboard /时,我可以查看index.php。

当我想使用GET方法时,我的网址将类似于localhost / project / dashboard /?status = active,我正尝试将其更改为localhost / project / dashboard / active或localhost / project / dashboard / status / active。

我在本地主机环境中工作,我的目录这样分解。

MAIN
- includes
- dashboard
|
-- subfolder
|-- somefile.php
|
-- index.php
- css
index.php
login.php
logout.php
.htaccess ** (this is the file im working with)

我正在使用以下网址:http://localhost/project/dashboard/active

我尝试将以下行添加到我的.htaccess中

1。

RewriteRule ^([a-zA-Z]+)/?$ /?status=$1 [NC,L,QSA] - doesn't work

找不到对象!错误404

2。

RewriteRule ^dashboard/([a-zA-Z]+)/?$ /?status=$1 [NC,L,QSA] - doesn't work

重定向到localhost / dashboard

3。

如果我在“ dashboard”目录中创建了另一个.htaccess并使用写1行

RewriteRule ^([a-zA-Z]+)/?$ index.php?status=$1 [NC,L,QSA] - WORKS

但是,当使用第三种方法时,我无法访问仪表板中的子文件夹或其文件。 因此,如果我尝试访问localhost / project / dashboard / subfolder / somefile,它将返回 “找不到对象!”错误404

如果我尝试访问localhost / project / dashboard / subfolder / somefile.php,它将重定向到localhost / project / dashboard /

稍后将有更多文件夹和子文件夹。

2 个答案:

答案 0 :(得分:0)

尝试

RewriteRule ^project/dashboard/(.*)$ project/dashboard/?status=$1 [L]

答案 1 :(得分:0)

在阅读汉娜的建议后,我调整了文件路径。

RewriteRule ^dashboard/([a-zA-Z]+)/?$ dashboard/?status=$1 [NC,L,QSA]

这似乎可以解决问题!

相关问题