php htaccess url 重写关于 request_uri 的混淆
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8194517/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
htaccess url rewrite confusion about request_uri
提问by Shaokan
I'm (sometimes) somehow managing to url rewriting successfully but I want to understand the core of what I'm actually doing.
我(有时)以某种方式成功地成功重写了 url,但我想了解我实际在做什么的核心。
Now, whenever I type http://localhost/admin/shop/
to the browser's address bar, I'd like my htaccess to rewrite the url as http://localhost/admin.php?page=shop
so in the php I can understand that I'm actually dealing with shop page. For this, I have:
现在,每当我http://localhost/admin/shop/
在浏览器的地址栏中键入内容时,我都希望我的 htaccesshttp://localhost/admin.php?page=shop
在 php 中重写 url,我可以理解我实际上是在处理商店页面。为此,我有:
RewriteEngine on
RewriteRule ^(.*)$ .php [nc]
RewriteCond %{REQUEST_URI} ^/admin\.php/[^/]+/$
RewriteRule ^admin\.php/(.*)/$ admin\.php?pval= [l,nc]
This works so far. But I'd like to understand why my code doesn't work when I do this:
到目前为止,这有效。但是我想了解为什么我的代码在执行此操作时不起作用:
###RewriteEngine on
###RewriteRule ^(.*)$ .php [nc]
### note the commenting out the code above
RewriteCond %{REQUEST_URI} ^/admin/[^/]+/$
RewriteRule ^admin/(.*)/$ admin?pval= [l,nc]
### note the removal of '\.php'
So basically, when you type 'http://localhost/admin/shop/' to the address bar, php's $_SERVER['REQUEST_URI']
will print out exactly /admin/shop/. Now, assuming php's REQUEST_URI
is like that, htaccess REQUEST_URI should be the same right? I don't really know if they are using different engines but that's what comes logical to me. So, assuming that I'm right, why the second example doesn't work when I remove the '.php' from the RewriteCond
and RewriteRule
? Also, if I'd had the chance to print the REQUEST_URI of htaccess, what would it actually print to the screen in the above example?
所以基本上,当你在地址栏中输入“http://localhost/admin/shop/”时,php$_SERVER['REQUEST_URI']
会准确地打印出/admin/shop/。现在,假设 phpREQUEST_URI
是这样,htaccess REQUEST_URI 应该是一样的吧?我真的不知道他们是否使用不同的引擎,但这对我来说是合乎逻辑的。所以,假设我是对的,为什么当我从RewriteCond
and 中删除 '.php' 时第二个例子不起作用RewriteRule
?另外,如果我有机会打印 htaccess 的 REQUEST_URI,那么在上面的示例中,它实际上会打印到屏幕上的是什么?
PS: I know that for this case, I don't really need to use htaccess as I can create a folder inside admin folder and name it shop and so on. But the thing is that I don't really have an admin folder as I'm using controllers and a simple switch in the admin.php to avoid creating millions of folders inside my application. This is just simpler to me.
PS:我知道对于这种情况,我真的不需要使用 htaccess,因为我可以在 admin 文件夹中创建一个文件夹并将其命名为 shop 等等。但问题是我没有真正的 admin 文件夹,因为我在 admin.php 中使用控制器和一个简单的开关来避免在我的应用程序中创建数百万个文件夹。这对我来说更简单。
回答by Mark Fox
§
§
The reason your amended rule doesn't work is probably because you omitted the .php
from the url-path. Assuming you have an admin.php
file hosted in the root this should work:
您修改后的规则不起作用的原因可能是因为您.php
从 url-path 中省略了。假设您admin.php
在根目录中托管了一个文件,这应该可以工作:
RewriteCond %{REQUEST_URI} ^/admin/[^/]+/$
RewriteRule ^admin/(.*)/$ /admin.php?pval= [L,NC]
§
§
Removing this
删除这个
RewriteRule ^(.*)$ .php [nc]
probably didn't cause any problems as it probably wasn't matching anything. The pattern ends with escaped dollar sign \$
which matches the $
character and not the end of line
meta-character. So that rule matches any path that contains a dollar sign.
可能没有引起任何问题,因为它可能不匹配任何东西。该模式以\$
匹配$
字符而不是end of line
元字符的转义美元符号结尾。因此该规则匹配任何包含美元符号的路径。
§
§
Is Apache's %{REQUEST_URI}
variable the same as PHP's $_SERVER['REQUEST_URI']
?
Apache 的%{REQUEST_URI}
变量与 PHP 的变量相同$_SERVER['REQUEST_URI']
吗?
More or less. But, for instance, if you did any rewriting with redirects you might get confused because the redirect will create a new, rewritten request. That's a specious example, but there could be other advanced use cases to watch out for. For simple cases, they are the same.
或多或少。但是,例如,如果您使用重定向进行任何重写,您可能会感到困惑,因为重定向将创建一个新的重写请求。这是一个似是而非的例子,但可能还有其他高级用例需要注意。对于简单的情况,它们是相同的。
回答by lfxgroove
Just a thought, i think the last slash doesn't let it match a .php file, ie: does http://localhost/admin/shop
give you the same page as http://localhost/admin/shop/
?
只是一个想法,我认为最后一个斜杠不允许它匹配 .php 文件,即:是否http://localhost/admin/shop
给你相同的页面http://localhost/admin/shop/
?
Try changing it to:
尝试将其更改为:
RewriteRule ^admin/(.*)$ admin\.php?pval= [l,nc]
回答by no_name_here
If you are using a .htaccess file to do mod_rewrite goodness, REQUEST_URI doesn't have the beginning slash, so for example /admin/shop/ -> admin/shop/
如果你使用 .htaccess 文件来做 mod_rewrite 的好事,REQUEST_URI 没有开头的斜杠,例如 /admin/shop/ -> admin/shop/
And to "debug" mod_rewrite you have to setup the "RewriteLog" directive in your conf file, something like this:
要“调试” mod_rewrite,您必须在 conf 文件中设置“RewriteLog”指令,如下所示:
RewriteLog "C:/wamp/logs/rewrite.log"
RewriteLogLevel 9