2012-02-19 162 views
0

我想一个url改变,如: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

怎么能解决呢

感谢

+1

你有什么其他重写规则?你必须发布详细信息。 – ThinkingMonkey 2012-02-20 06:16:13

+0

'RewriteEngine叙述在 的RewriteCond%{REQUEST_FILENAME}!-f 的RewriteCond%{REQUEST_FILENAME}!-d 重写规则^(。*)/?$ home.php?p = 1时$ [L,NS]' – user1219391 2012-02-20 08:56:12

+0

我在这里没有任何错误。你确定你没有错过任何其他细节?并通过编辑问题本身来更新任何细节。 – ThinkingMonkey 2012-02-20 14:36:36

回答

0

让我们来看看你重写第一:

RewriteEngine On 

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

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

其次,你的规则是倒退,如果你想重写home.php网址为site/index一个,你必须把home.php比赛的左侧,右侧的site/index

RewriteRule ^home.php?p=(.*) /site/$1 

请注意,我有一个绝对重写。这意味着mod_rewrite将通过在其上覆盖http://example.com来重写重写的URL。一个新的请求现在在内部生成,用于http://example.com/site/<whatever>。由于我们没有相对重写,因此我们可以不使用RewriteBase而离开。

至于你的最后一个问题,目前还不清楚为什么当你访问localhost/site/home.php?p=profile.user你被带到localhost/profile.user。我怀疑它可能是你的home.php脚本。您正在尝试使用mod_rewrite劫持该特定类型的PHP请求并将其发送到其他地方,对吗?

+0

我认为他没有正确表达自己。很多人实际上这样做:不是说“我想将'localhost/site'重写为'home.php?blabla',而是说相反,这是一个错误和错误。 – 2012-03-12 09:11:35

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]