简体   繁体   English

RewriteRule - 或 document_root 到子目录(不是重定向)的别名会导致“错误请求”或“内部服务器错误”

[英]RewriteRule -or Alias for document_root to subdurectory (not redirect) causes either `Bad Request` or `Internal Server Error`

After exhaustive searching on SO and SF I find this rather concerning.在对 SO 和 SF 进行详尽搜索后,我发现这相当令人担忧。 All of the related answers causes Apache 2.4 to just break and I have no idea what I'm doing wrong, it should be really simple.所有相关的答案都会导致 Apache 2.4 崩溃,我不知道我做错了什么,它应该非常简单。

For brevity, here is the code, though I've tested each separately, not all in 1 go as below:为简洁起见,这是代码,尽管我已经分别测试了每个代码,但并非全部都在 1 中,如下所示:

    RewriteRule ^(.*)$ .anon.dir/Repo/data/native/fuse/$1 [L]
    ## Bad Request

    RewriteRule .* .anon.dir/Repo/data/native/fuse/
    ## Bad Request

    RewriteRule ^(.*)$ /.anon.dir/Repo/data/native/fuse/$1 [L]
    ## Internal Server Error

    RewriteRule ^(.*)$ - [E=TRGT:".anon.dir/Repo/data/native/fuse/index.php"]
    RewriteRule ^(.*)$ %{ENV:TRGT}
    ## Bad Request

    ## Alias / %{DOCUMENT_ROOT}/blah ... for vHost/apache conf only
    ## Internal Server Error - in htaccess

The actual code is in an <if> block, this exactly:实际代码在<if>块中,这正是:

<If "%{QUERY_STRING} =~ /(^|&)ANONFUSETEST($|&)/">
    Options -MultiViews
    RewriteEngine on

    RewriteRule ^(.*)$ .anon.dir/Repo/data/native/fuse/$1 [L]
</If>

I'm using Apache 2.4 and the target subdirectory has no htaccess in it, only an index.php file with <h1>It Works!</h1> inside it .. This is on a shared host at a respectable hosting company, so there should be no funny business going on elsewhere.我正在使用 Apache 2.4 并且目标子目录中没有htaccess ,只有一个index.php文件,里面有<h1>It Works!</h1> .. 这是在一个受人尊敬的托管公司的共享主机上,所以其他地方不应该发生有趣的事情。

Here are some useful links at ServerFault and StackOverflow and one more .. although as mentioned, it doesn't work .. or I'm doing something wrong, which is most probably the case.以下是ServerFaultStackOverflow上的一些有用链接,还有一个.. 尽管如上所述,它不起作用 .. 或者我做错了什么,这很可能是这种情况。

Does it have to do with the .是否与. at the start of the path?在路径的起点? -that it may be seen as "hidden"? - 它可能被视为“隐藏”? .. I'm out of ideas :/ ..我没有想法:/

Any help will be appreciated and rewarded in kind, thanks in advance.任何帮助将不胜感激并给予实物奖励,提前致谢。

The working solution is rather simple, have a look, elaboration follows:工作方案相当简单的,看看,阐述如下:

    RewriteEngine on

    RewriteCond %{REQUEST_URI} !/.anon.dir/Repo/data/native/fuse/
    RewriteRule ^(.*)$ /.anon.dir/Repo/data/native/fuse/ [L,QSA]

There seems to be some internal safety check that enforces a condition that makes sure the request is not the same as the target, additionally:似乎有一些内部安全检查会强制执行一个条件,以确保请求与目标不同,另外:

  • the target-path starts and ends with a slash目标路径斜杠开头和结尾
  • appending $1 to the target will result in 404 , so QSA is used instead$1附加到目标将导致404 ,因此使用QSA
  • if you want to define the path once, you're out of luck, or so it seems如果你想定义一次路径,你就不走运了,或者看起来

Assigning the path in a variable and using it this way will cause issues, like the following:在变量中分配路径并以这种方式使用它会导致问题,如下所示:

<If "%{QUERY_STRING} =~ /(^|&)ANONFUSETEST($|&)/">
    RewriteEngine on

    RewriteRule ^(.*)$ - [E=TRGT:/.anon.dir/Repo/data/native/fuse/]

    RewriteCond %{REQUEST_URI} !%{ENV:TRGT}
    RewriteRule ^(.*)$ %{ENV:TRGT} [L,QSA]

    ## result is `Bad Request`
</If>

If you know how to make this more "dynamic" eg: using the path only once, then your answer will be preferable.如果您知道如何使其更加“动态”,例如:仅使用一次路径,那么您的答案将更可取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM