Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.htaccess 尾随斜杠vs无尾随斜杠无重定向_.htaccess_Apache2 - Fatal编程技术网

.htaccess 尾随斜杠vs无尾随斜杠无重定向

.htaccess 尾随斜杠vs无尾随斜杠无重定向,.htaccess,apache2,.htaccess,Apache2,我必须能够在收到GET或POST请求时指导它 /eSCL/ScannerStatus 或 到 我不是在寻找一个重定向,因为这是我现在得到的。(301永久移动) 也就是说,请求是否引用了一个目录,我需要在该目录中加载index.php,而不需要重定向 我已经看过这里的文件了 坦率地说,它似乎并没有像上面所说的那样起作用,或者也许我错过了一个mod 我试过很多变体 我跑了 sudo a2enmod rewrite 及 两者现在都显示为已启用 在Raspberry Pi3上运行的Apache 2.

我必须能够在收到GET或POST请求时指导它

/eSCL/ScannerStatus

我不是在寻找一个重定向,因为这是我现在得到的。(301永久移动)

也就是说,请求是否引用了一个目录,我需要在该目录中加载index.php,而不需要重定向

我已经看过这里的文件了 坦率地说,它似乎并没有像上面所说的那样起作用,或者也许我错过了一个mod

我试过很多变体

我跑了

sudo a2enmod rewrite

两者现在都显示为已启用

在Raspberry Pi3上运行的Apache 2.4

我尝试过很多不同的规则,我也在谷歌搜索结果中看到,这对于其他尝试做同样事情的人来说是个问题。我看到许多“解决方案”张贴在网上,其中许多都不起作用

我看不出有任何理由对我所做的20多次修改尝试施加压力,但都失败了。我正在寻找什么工作没有重定向

编辑更新-------------------------------------------------

我刚刚回到这里,删除了
.htaccess
,现在服务器拒绝加载
index.php
,而是显示了一个目录列表

这让我感到困惑。我不认为我需要
.htaccess
来获取PHP文件,因为它以前不存在

编辑更新2------------------------------------ 我设法回到了正轨,一些指令似乎因为某种原因而消失了

我现在有了in/var/www/html/.htaccess

DirectorySlash Off

RewriteEngine On

RewriteRule ^eSCL/ScannerStatus/?$ eSCL/ScannerStatus/index.php [L]

RewriteRule ^eSCL/ScannerCapabilities/?$ eSCL/ScannerCapabilities/index.php [L]

RewriteRule ^eSCL/ScanJobs/?$ eSCL/ScanJobs/index.php [L]

RewriteRule ^eSCL/Scans/?$ eSCL/Scans/index.php [L]
在apache2.conf的全局版本中(显然,我必须这样做才能使index.php文件再次作为默认文件加载):

我还核实了:

pi@raspberrypi:/etc/apache2 $ sudo a2enmod rewrite
Module rewrite already enabled
pi@raspberrypi:/etc/apache2 $ sudo a2enmod dir
Module dir already enabled
如果
ScannerStatus
是一个物理目录,那么默认情况下,mod_dir将使用301重定向附加斜杠,以便“修复”URL

您需要覆盖此行为并设置
DirectorySlash Off
。然后需要使用mod_rewrite将请求内部重写为所需的
index.php
文档(而不是像通常那样依赖
DirectoryIndex

例如,在根
.htaccess
文件中:

DirectorySlash Off

RewriteEngine On

RewriteRule ^eSCL/ScannerStatus/?$ eSCL/SCannerStatus/index.php [L]
请注意,您需要清除浏览器缓存,因为301重定向以附加斜杠(通过mod_dir)很可能已被浏览器缓存

我不认为我需要
.htaccess
来获取PHP文件,因为它以前不存在

如果要覆盖默认服务器行为并使用不带斜杠的物理目录,则需要
.htaccess


更新:

如果您有权访问服务器配置,则不需要
.htaccess
.htaccess
(目录上下文)中的所有指令都可以直接进入相关的
容器中。(尽管开发/分发需求可以使
.htaccess
文件的使用更容易。)

顺便提一下,
DirectoryIndex
指令可以在
.htaccess
中使用


旁白:

如前所述,上面在
.htaccess
中的重写否定了对
DirectoryIndex.php
的需要。但是,可以稍微修改它们以利用这一点。例如(一项学术活动):

现在,使用mod_rewrite将
/eSCL/ScannerStatus
(无尾随斜杠)请求重写为
index.php
。但是对
/eSCL/ScannerStatus/
(带尾随斜杠)的请求现在由
DirectoryIndex
处理,它通过内部子请求返回
index.php
。使用早期的
重写规则
,这两个请求都由mod_rewrite处理

请注意,4个不同子目录的4条规则可能组合为一条规则

如果
ScannerStatus
是一个物理目录,那么默认情况下,mod_dir将使用301重定向附加斜杠,以便“修复”URL

您需要覆盖此行为并设置
DirectorySlash Off
。然后需要使用mod_rewrite将请求内部重写为所需的
index.php
文档(而不是像通常那样依赖
DirectoryIndex

例如,在根
.htaccess
文件中:

DirectorySlash Off

RewriteEngine On

RewriteRule ^eSCL/ScannerStatus/?$ eSCL/SCannerStatus/index.php [L]
请注意,您需要清除浏览器缓存,因为301重定向以附加斜杠(通过mod_dir)很可能已被浏览器缓存

我不认为我需要
.htaccess
来获取PHP文件,因为它以前不存在

如果要覆盖默认服务器行为并使用不带斜杠的物理目录,则需要
.htaccess


更新:

如果您有权访问服务器配置,则不需要
.htaccess
.htaccess
(目录上下文)中的所有指令都可以直接进入相关的
容器中。(尽管开发/分发需求可以使
.htaccess
文件的使用更容易。)

顺便提一下,
DirectoryIndex
指令可以在
.htaccess
中使用


旁白:

如前所述,上面在
.htaccess
中的重写否定了对
DirectoryIndex.php
的需要。但是,可以稍微修改它们以利用这一点。例如(一项学术活动):

现在,使用mod_rewrite将
/eSCL/ScannerStatus
(无尾随斜杠)请求重写为
index.php
。但是对
/eSCL/ScannerStatus/
(带尾随斜杠)的请求现在由
DirectoryIndex
处理,它通过内部子请求返回
index.php
。使用早期的
重写规则
,这两个请求都由mod_rewrite处理

请注意,4个不同子目录的4条规则可以组合成一条规则。

请始终
<Directory "/var/www/html">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/scans/*">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/eSCL/ScannerStatus">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/eSCL/ScannerCapabilities">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/eSCL/ScanJobs">
DirectoryIndex  index.php index.htm
</Directory>
HTTP/1.1 301 Moved Permanently
Date: Thu, 06 Feb 2020 16:56:00 GMT
Server: Apache/2.4.25 (Raspbian)
Location: http://192.168.1.50/eSCL/ScannerStatus/
Content-Length: 327
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a 
href="http://192.168.1.50/eSCL/ScannerStatus/">here</a>.</p>
<hr>
<address>Apache/2.4.25 (Raspbian) Server at 192.168.1.50 Port 80</address>
# Include the virtual host configurations:
# IncludeOptional sites-enabled/*.conf
pi@raspberrypi:/etc/apache2 $ sudo a2enmod rewrite
Module rewrite already enabled
pi@raspberrypi:/etc/apache2 $ sudo a2enmod dir
Module dir already enabled
/eSCL/ScannerStatus
DirectorySlash Off

RewriteEngine On

RewriteRule ^eSCL/ScannerStatus/?$ eSCL/SCannerStatus/index.php [L]
<Directory "/var/www/html">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/scans/*">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/eSCL/ScannerStatus">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/eSCL/ScannerCapabilities">
DirectoryIndex  index.php index.htm
</Directory>

<Directory "/var/www/html/eSCL/ScanJobs">
DirectoryIndex  index.php index.htm
</Directory>
<Directory "/var/www/html">
# Allow .htaccess files to override config directives
AllowOverride All

# Allow mod_dir to issue an internal subrequest for the DirectoryIndex
# when requesting /subdirectory/ (note the trailing slash)
DirectoryIndex  index.php index.htm

# Access needs to be permitted (somewhere)
Require all granted
</Directory>
DirectoryIndex index.php
DirectorySlash Off

RewriteEngine On

# Note the different regex having removed "/?"
RewriteRule ^eSCL/ScannerStatus$ eSCL/SCannerStatus/index.php [L]