htaccess和url重定向

时间:2012-01-06 04:49:31

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

我正在研究MVC PHP应用程序。

我的.htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

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

我的index.php:

<?php

$url = $_REQUEST['url'];

echo $url;

因此,当我在url中写入类似(mysite.com/mvc/help)的内容时,php会在主页上打印(帮助)。它在我在url中写的所有东西都很好用(mysite.com/mvc/index/log)返回(索引/日志),但如果我写(mysite.com/mvc/index)那么我没有错误但是没有显示,$ url未设置!

我希望我解释得对。 干杯

编辑:我犯了一个错误,(mysite.com/mvc/index/log)什么也没有回复。似乎第一个单词是“index”它不起作用,如果url就像(mysite.com/mvc/log/index)它工作并返回(log / index)。

另外我在index.php(print_r($ _ GET);)中添加了一行,当url类似时返回一个空数组(mysite.com/mvc/index/log或mysite.com/mvc/索引),如果这可以帮助......

经过一番调查后,我认为问题是“索引”这个词是用“.html”或“.php”自动完成的,我不知道怎么摆脱......


解决方案: 这是我的htaccess工作:

Options -Indexes +FollowSymLinks -Multiviews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

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

2 个答案:

答案 0 :(得分:1)

好的我已经找到了如何使它工作,似乎我需要这一行:

Options -Indexes +FollowSymLinks -Multiviews

答案 1 :(得分:0)

尝试将其更改为:

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

并将您的$_REQUEST更改为$_GET

$url = $_GET['url'];