在 .htaccess 中启用人类可读的 URL

Enable human readable URL's in .htaccess

前言:是的,这个问题好像重复了,我找到了相关问题,但那里的答案对我没有帮助。 :(

您好,我想为我的 PHP 项目添加人类可读的 URL 支持。现在我的 URL 查询字符串看起来像:

index.php?url=main/index

我想让它看起来像:

index.php/main/index

我阅读了以下文章:

Whosebug

Cheatsheet

Whosebug

Whosebug

但是当我这样做时:

var_dump($_GET['url']); // get empty array

,获取空数组,如未添加 url 参数。

我现在的 .htaccess:

DirectoryIndex index.php

Options +FollowSymLinks

RewriteEngine On

RewriteBase /
RewriteRule ^(.*)$ index.php?url= [NC]

有人可以帮帮我吗?谢谢!

排队
RewriteRule ^(.*)$ index.php?url= [NC] (.*) 匹配 url 的部分,直到 '?'查询开始的地方。
要使用查询中传递的值,您需要 QSA 标志。表示查询字符串追加。

URL: http://domain.com/index.php/controller/action

重写URL:http://domain.com/index.php?url=controller/action

.htaccess

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule ^index.php/(.*)$ /index.php?url= [L,QSA]

解释:

模式 ^index.php/(.*)$ 中的 .* 匹配传入 URL 上 index.php/ 之后的所有内容。括号有助于将部分捕获为变量 </code>,然后将其添加到替换的末尾 URL <code>/index.php?url= + </code>.</p> <p><code>[L, QSA]: L 忽略其他重写规则,如果适合的话。 QSA 表示查询字符串追加。

你有

index.php?url=main/index

你想重写(然后用户重定向)到这个

index.php/main/index

试试这个怎么样?

DirectoryIndex index.php

Options +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]

R=301 或 302 取决于您的需要

在这个例子中,我假设在原来的 url 中只有 a-z 字符。 澄清一下(由于通常过程是相反的)用户将被重定向,此规则不会转换您页面上的链接。

试试下面的代码,只考虑 index.php 你可以根据你的要求替换它。如果您需要进一步的帮助,请告诉我。

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=

对我有用的是:- <?php var_dump($_GET); ?> 这是我在 url http://localhost/baba.php/abcd 上做的唯一一件事。和原始 .htaccess 文件

DirectoryIndex baba.php RewriteEngine ON RewriteBase / RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=

如果你的 URL : www.abcd.com/index.php/abcd/...

.htaccess

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