【问题标题】:Pretty URL with $_GET method带有 $_GET 方法的漂亮 URL
【发布时间】:2019-08-16 21:32:59
【问题描述】:

我是编码新手,因此非常感谢您提供额外的解释。到目前为止,我能够使用我的 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 方法时,我的 url 将类似于 localhost/project/dashboard/?status=active,我试图将其更改为 localhost/project/dashboard/active 或 localhost/project/dashboard/status /主动。

我在 localhost 环境中工作,我的目录是这样分解的。

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

重定向到本地主机/仪表板

3.

如果我在“仪表板”目录中创建另一个 .htaccess 并使用 write 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/

以后会有更多的文件夹和子文件夹。

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    试试

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

    【讨论】:

    • 很遗憾,内部服务器错误 - 服务器遇到内部错误或配置错误,无法完成您的请求。
    【解决方案2】:

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

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

    这似乎可以解决问题!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-04
      • 2014-09-24
      • 2013-07-09
      • 1970-01-01
      • 2018-04-06
      • 2020-01-02
      • 2012-05-07
      • 2013-08-30
      相关资源
      最近更新 更多