2011-07-23 124 views
0

我刚刚开始使用的.htaccess mod_rewrite我发现自己难倒:如何将index.php重定向到index.php?route = home,并将其重写为/ home?

我建立基于查询字符串加载意见后index.php的应用程序(使用MVC模式);例如。 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

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

回答

3

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

Tips and Tricks

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

所以这个环节的好去处:

http://www.example.com/home 

会重定向到

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

UPDATE:

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

使用条件将确保您的CSS和JavaScript犯规打破

编辑:由于通过Shango评论,我改变/^(.*)/^(.*)

+0

我试过'RewriteEngine On'然后'RewriteRule /^(.*)/ index.php? route = $ 1 [L]',但在“http:// domain.com/home”中输入时显示“在此服务器上未找到请求的URL/home”。我还需要添加一个默认的查询字符串,而不仅仅是'domain.com/home'重定向。 – jlmakes

+0

用于测试将'[L]'更改为'[R,L]'并告诉我地址​​栏上的网址是什么 – Ibu

+1

RewriteRule ^(。*)index.php?route = $ 1 [L,QSA] this should追加查询字符串 –

1

试试这个:

RewriteEngine ON 

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

抱歉没有注意到第一个答案已更新,也许第一个答案更新应该更好。只是QSA –

+0

工程很棒。你的增加是获得这个工作的关键。 – jlmakes

相关问题