0

我正在与我的班级一起处理一个似乎不起作用的 htaccess 文件的项目。我有这个:

SetEnvIf Request_URI ^ root=/myroot/

RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index    
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$                      %{ENV:root}index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]

其实我不明白的地方有三点:

如果我将 url 查询为/section/function/,我应该得到 /myroot/index.php?type=section&action=function 但我进入localhost/section/function浏览器,导致错误。url 是否双向翻译:myroot/index.php?type=section&action=function 在浏览器中显示为/section/function/. 如果我删除最后一行,我会得到空白错误页面,为什么会这样?

编辑 :

如此符合 Olaf Dietsche 所说的,我将规则更改为

SetEnvIf Request_URI ^ root=/myroot/

RewriteRule ^(assets|inc) - [L]
RewriteRule ^section[/]?$ %{ENV:root}section/index    
RewriteRule ^section/function/(.*)$ %{ENV:root}?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$                      %{ENV:root}?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]

但是当我单击时<a href="section/somefunction"></a>,我会看到一个页面localhost/section/some function。我真的不知道怎么回事......

4

1 回答 1

0

首先,你必须有

RewriteEngine on

在 .htaccess 中以使规则生效。

规则重写请求,但不重定向客户端。两者的区别在于

  • rewrite将另一个页面的内容发送给客户端,而不告诉客户端。客户端认为,它获得了请求页面的内容。
  • 重定向向客户端发送另一个 URL,导致客户端使用此 URL 发出新请求。客户端知道(并显示在浏览器的栏中)它收到了另一个页面的内容。

如果要重定向客户端,必须RRewriteRule

RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [R,L]

更新

您必须在替换之前删除%{ENV:root},因为 URL 是 eg/index.php而不是/myroot/index.php

RewriteRule ^section[/]?$ /section/index    
RewriteRule ^section/function/(.*)$ /index.php?type=section&action=function [L]
RewriteCond %{QUERY_STRING} ^(.*)?$
RewriteRule ^section/(.*)$ /index.php?type=section&action=view [L]
RewriteRule ^index\.php$ - [L]
于 2013-04-17T09:43:30.747 回答