我正在使用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]
事实上,有三件事我不明白:
如果我将网址查询为/section/function/
,我应该获得/myroot/index.php?type=section&action=function但我的浏览器中会显示localhost/section/function
,从而导致错误。该网址是否已翻译为bidirectionnaly: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
。我真的不知道出了什么问题......
答案 0 :(得分:0)
首先,你必须
RewriteEngine on
在.htaccess中使规则有效。
规则重写请求,但不要重定向客户端。两者之间的区别是
如果要重定向客户端,则必须在RewriteRule
R
标记
RewriteRule ^section/function/(.*)$ %{ENV:root}index.php?type=section&action=function [R,L]
<强>更新强>:
您必须在替换前删除%{ENV:root}
,因为该网址例如是/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]