【问题标题】:Get URI params without index.php获取不带 index.php 的 URI 参数
【发布时间】:2019-08-03 18:20:38
【问题描述】:

我有一个类似http://example.com/index.php/fruit/1a2b3c 的网址

我想获取我已为其编写代码的 URI。

现在我想从可见的 URL 中删除 index.php,即使 URL 是 http://example.com/fruit/1a2b3c 它也应该可以工作并且它应该仍然指向 index.php

我在 apache2 上使用 PHP 7

【问题讨论】:

标签: php apache .htaccess mod-rewrite apache2


【解决方案1】:

将以下代码添加到您的 .htaccess 文件中。

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

【讨论】:

  • 嘿,非常感谢您的回答,它确实解决了我的问题,您能否逐行解释它是如何工作的?我发现很难理解它是什么以及如何做到这一点的?
【解决方案2】:

将您的 .htaccess 文件更改为,

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

希望这会奏效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2021-03-27
    • 2017-05-23
    • 2018-05-08
    • 1970-01-01
    • 2011-01-29
    • 1970-01-01
    相关资源
    最近更新 更多