我刚开始使用。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
。
任何帮助将不胜感激!谢谢。
这里是学习如何有效使用.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的评论,我将/^(.*)/
更改为^(.*)
试试这个:
RewriteEngine ON
RewriteCond %{REQUEST_URI} !^/(.*)/(.+)
RewriteRule ^([^/]*) index.php?route=$1 [L,QSA]