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

时间:2015-10-10 20:05:42

标签: php apache .htaccess mod-rewrite url-rewriting

前言:是的,这个问题似乎重复了,我发现了相关的问题,但那里的答案对我没有帮助。 :(

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

index.php?url=main/index

我想让它看起来像:

index.php/main/index

我阅读了以下文章:

Stackoverflow

Cheatsheet

Stackoverflow

Stackoverflow

但是当我这样做时:

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

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

我当前的.htaccess

DirectoryIndex index.php

Options +FollowSymLinks

RewriteEngine On

RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]
是的,有人能帮帮我吗?谢谢!

5 个答案:

答案 0 :(得分:5)

网址:http://domain.com/index.php/controller/action

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

.htaccess

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

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

说明:

模式.*中的^index.php/(.*)$匹配传入网址上index.php/之后的所有内容。括号有助于将部件捕获为变量$1,然后将其添加到替换网址/index.php?url= + $1的末尾。

[L, QSA]: 如果这适合,我会忽略其他重写规则。 QSA表示查询字符串追加。

答案 1 :(得分:2)

你有

index.php?url=main/index

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

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取决于您的需要

在这个例子中,我假设在原始网址上只有a-z字符。 只是为了澄清(由于一般情况下该过程是反向的)用户将被重定向,此规则不会转换页面上的链接。

答案 2 :(得分:1)

在线
RewriteRule ^(.*)$ index.php?url=$1 [NC](。*)将网址的部分与“?”匹配查询开始的地方 要使用查询中传递的值,您需要QSA标志。表示查询字符串追加。

答案 3 :(得分:0)

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

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

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

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

答案 4 :(得分:0)

如果您的网址:www.abcd.com/index.php/abcd /...

.htaccess

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