我如何将index.php重定向到index.php?route = home,并将其重写为/ home?

时间:2011-07-23 05:40:09

标签: php .htaccess

我刚刚开始使用.htaccess mod_rewrite,我发现自己很难过:

我正在构建一个应用程序(使用MVC模式),它根据查询字符串将视图加载到index.php;例如。 domain.com/index.php?route=home将加载主页。

但是,这意味着domain.com/index.php没有任何查询字符串加载......或更多内容,domain.com不加载任何内容。

。 。

我想要的是domain.com(以及domain.com/index.php

重定向到domain.com/index.php?route=home

然后将domain.com/index.php?route=home重写为domain.com/home

任何帮助将不胜感激!感谢。

2 个答案:

答案 0 :(得分:3)

这是学习如何有效使用.htaccess

的好地方

Tips and Tricks

RewriteRule ^(.*) index.php?route=$1 [L]

所以这个链接:

http://www.example.com/home

将重定向到

http://www.example.com/index.php?route=home

<强>更新

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?route=$1 [L]

使用该条件将确保您的css和javascript不会中断

编辑:由于评论由Shango,我将/^(.*)/更改为^(.*)

答案 1 :(得分:1)

试试这个:

RewriteEngine ON

RewriteCond %{REQUEST_URI} !^/(.*)/(.+)
RewriteRule ^([^/]*) index.php?route=$1 [L,QSA]