mod 用点重写
我想将 url 更改为: localhost/site/home.php?p=index
到 localhost/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
那么我该如何修复它
谢谢
i want to change a url like : localhost/site/home.php?p=index
to localhost/site/index
i use this code in my htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ home.php?p=$1 [L,NS]
but when i write like localhost/site/home.php?p=profile.user
i get the 404 error, and go to this link
localhost/profile.user
so how can i fix it
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
让我们首先看看您的重写:
这是相对重写:替换文本
home.php...
不以斜杠开头。每个目录上下文(
或.htaccess
)中的相对重写需要配置RewriteBase
指令,否则它们会执行错误的事情。其次,你的规则是倒退的,如果你想将
home.php
URL 重写为site/index
,你必须将home.php< /code> 匹配在左侧,
site/index
在右侧:请注意,我进行了绝对重写。这意味着 mod_rewrite 将通过在重写中粘贴
http://example.com
来创建一个 URL。现在会在内部生成http://example.com/site/
的新请求。我们可以在不使用 RewriteBase 的情况下逃脱,因为我们没有相对的重写。至于你的最后一个问题,目前尚不清楚为什么当你访问 localhost/site/home.php?p=profile.user 时你会被带到 localhost/profile.user代码>.我怀疑可能是您的
home.php
脚本在执行此操作。您正在尝试使用mod_rewrite
劫持特定类型的 PHP 请求并将其发送到其他地方,对吧?Let's look at your rewrites first:
This is relative rewrite: the replacement text
home.php...
does not begin with a slash. Relative rewrites in a per-directory context (<Directory>
or.htaccess
) require aRewriteBase
directive to be configured, otherwise they do the wrong thing.Secondly, your rule is backwards, If you want to rewrite the
home.php
URL to thesite/index
one, you have to put thehome.php
match on the left side, and thesite/index
on the right:Notice that I have an absolute rewrite. This means that mod_rewrite will create a URL out of the rewrite by sticking
http://example.com
on it. A new request is internally generated now forhttp://example.com/site/<whatever>
. We can get away without usingRewriteBase
since we have no relative rewrites.As for your last question, it is not clear why when you access
localhost/site/home.php?p=profile.user
you're being taken tolocalhost/profile.user
. I'm suspecting that it's yourhome.php
script doing that, perhaps. You're trying to usemod_rewrite
to hijack that particular kind of PHP request and send it elsewhere, right?您的意思可能是:您想以这种方式重写:
所以这应该有效
现在如果您想要相反:
这应该有效:
What you meant is probably: you want to rewrite this way:
So this should work
Now if you want the opposite:
This should work: