.htaccess RewriteRule domain.com/page/page到domain.com?p=page/page

时间:2011-07-04 23:06:07

标签: apache .htaccess

我正在尝试设置我的.htaccess文件。我将如何得到这个结果:

请求:domain.com/page/page

重写为:domain.com/index.php?p=page/page

我试过了:

RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

没有运气。

2 个答案:

答案 0 :(得分:1)

好的,我把它解决了。我需要两个规则:

RewriteRule ^([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1
RewriteRule ^([a-zA-Z0-9_\\s-]+)/([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1/$2

我的php脚本不喜欢它,因为只有

RewriteRule ^([a-zA-Z0-9_\\s-]+)(/)?$ index.php?p=$1

domain.com/no1/会转换为domain.com/index.php?p=no1/< ---无法理解结束斜杠

这个解决方案唯一的问题是它只有两个虚拟目录,但是我只需要它就可以了。 这是我第一次使用.htaccess,所以我很抱歉我缺乏理解。

答案 1 :(得分:0)

在.htaccess文件中尝试此代码:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteRule ^(?!index\.php)(.*)$ /index.php?p=$1 [L,NC,QSA]

否定前瞻将避免无限循环。

相关问题