mod用点重写

时间:2012-02-19 22:23:12

标签: .htaccess mod-rewrite apache2

我想将网址更改为:localhost/site/home.php?p=indexlocalhost/site/index

我在我的htaccess文件中使用此代码

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS] 

但是当我像localhost/site/home.php?p=profile.user那样写时,我得到404错误,并转到此链接

  

本地主机/ profile.user

所以我该怎样修理它?
谢谢

2 个答案:

答案 0 :(得分:0)

让我们先来看看你的重写:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS] 

这是 relative 重写:替换文本home.php...不以斜杠开头。每个目录上下文(<Directory>.htaccess)中的相对重写需要配置RewriteBase指令,否则它们会执行错误操作。

其次,您的规则是向后的,如果您要将home.php网址重写为site/index,则必须在左侧放置home.php匹配,并且{ {1}}在右边:

site/index

请注意,我有一个绝对重写。这意味着mod_rewrite将通过在其上粘贴RewriteRule ^home.php?p=(.*) /site/$1 来创建重写之外的URL。现在为http://example.com内部生成了一个新请求。我们可以在不使用http://example.com/site/<whatever>的情况下离开,因为我们没有相对重写。

至于您的上一个问题,我们不清楚为什么当您访问RewriteBase时,您被带到localhost/site/home.php?p=profile.user。我怀疑是你的localhost/profile.user脚本也是这样做的。您正试图使用​​home.php来劫持那种特定类型的PHP请求并将其发送到其他地方,对吗?

答案 1 :(得分:0)

你的意思可能是:你想用这种方式改写:

http://mysite.com/index => http://mysite.com/home.php?p=index

所以这应该有用

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^/?$ /home.php?p=$1 [QSA,L] 

现在,如果你想要相反的

http://mysite.com/home.php?p=index => http://mysite.com/index

这应该有效:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule /home\.php$ / [QSA,L]