url重写index.php

时间:2010-04-16 12:01:13

标签: php mod-rewrite url-rewriting

我有像

这样的网址
http://mysite.com/index.php?p=resources
http://mysite.com/index.php?p=resources&s=view&id=938

但我想要像

这样的网址
http://mysite.com/resources
http://mysite.com/resources/view/938

而不是制作数百条重写规则我不知道是否有可能只有一个?我可以通过“获取uri并将其拆分为部分”来实现这一点,然后只需为index.php添加重写规则

但是怎么样?有人可以给出一个例子或链接教程

2 个答案:

答案 0 :(得分:4)

我碰巧在.htaccess中使用它:

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

RewriteRule .* index.php [L]

无论请求什么,这基本上都会调用index.php。然后在PHP代码中,你可以查看$ _SERVER ['REQUEST_URI']来获取URL并相应地解析它。

RewriteCond行可以排除对文件的直接调用。在我的情况下,出于性能原因,我不希望像js / css / image文件的请求通过index.php。

答案 1 :(得分:2)

创建.htaccess文件。不是somefilename.htaccess,它简单地命名为.htaccess 注意:您的index.php和.htaccess文件应位于同一目录中。

现在在你的.htaccess文件上试试这个

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9\-]+)$ index.php?p=resources
RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+)/([0-9]+)$ index.php?p=resources&s=view&id=938
</IfModule>

详细了解网址重写here

相关问题