mod 用点重写

发布于 2025-01-06 18:11:22 字数 463 浏览 6 评论 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

那么我该如何修复它

谢谢

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月棠 2025-01-13 18:11:22

让我们首先看看您的重写:

RewriteEngine On 

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

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

其次,你的规则是倒退的,如果你想将 home.php URL 重写为 site/index ,你必须将 home.php< /code> 匹配在左侧,site/index 在右侧:

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

请注意,我进行了绝对重写。这意味着 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:

RewriteEngine On 

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

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 a RewriteBase 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 the site/index one, you have to put the home.php match on the left side, and the site/index on the right:

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

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 for http://example.com/site/<whatever>. We can get away without using RewriteBase 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 to localhost/profile.user. I'm suspecting that it's your home.php script doing that, perhaps. You're trying to use mod_rewrite to hijack that particular kind of PHP request and send it elsewhere, right?

又怨 2025-01-13 18:11:22

您的意思可能是:您想以这种方式重写:

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] 

What you meant is probably: you want to rewrite this way:

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

So this should work

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

Now if you want the opposite:

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

This should work:

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule /home\.php$ / [QSA,L] 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文