Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.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
apache重写的URL包含完整的Linux路径_Linux_Apache_.htaccess_Url_Mod Rewrite - Fatal编程技术网

apache重写的URL包含完整的Linux路径

apache重写的URL包含完整的Linux路径,linux,apache,.htaccess,url,mod-rewrite,Linux,Apache,.htaccess,Url,Mod Rewrite,我正试图在.htaccess文件中使用以下Apache重写规则来去除URL中的尾部斜杠。我在多个论坛的多个线程中看到了此解决方案: RewriteRule ^(.*)/$ $1 [L,R=301] 这会导致URL错误 http://www.domain.com/widgets/ 改写为 http://www.domain.com/usr/local/www/apache22/data/domain/widgets RewriteRule前面是一些显式301重定向,后面是其他重写条件和规则,

我正试图在.htaccess文件中使用以下Apache重写规则来去除URL中的尾部斜杠。我在多个论坛的多个线程中看到了此解决方案:

RewriteRule ^(.*)/$ $1 [L,R=301]
这会导致URL错误

http://www.domain.com/widgets/
改写为

http://www.domain.com/usr/local/www/apache22/data/domain/widgets
RewriteRule前面是一些显式301重定向,后面是其他重写条件和规则,但即使它是.htaccess文件中的唯一语句,也会发生同样的情况

这个问题发生在我的linux服务器和XAMPP服务器上。在windows中,URL被重写为

http://www.domain.com/C:/xampp/htdocs/domain/widgets
如果我从规则中删除“L”,301将记录在apache日志中,但不会发生重写

以下是此域的vhost条目:

<VirtualHost X.X.X.X:80>
    ServerAdmin admin@domain.com
    DocumentRoot "/usr/local/www/apache22/data/domain"
    ServerName www.domain.com
    ErrorLog "/var/log/apache22/domain-error.log"
    CustomLog "/var/log/apache22/domain-access.log" combined
    <Directory /usr/local/www/apache22/data/domain>
        AllowOverride FileInfo
    </Directory>
    <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresDefault "access plus 1 week"
        ExpiresByType text/html "access plus 1 week"
        ExpiresByType image/gif "access plus 1 week"
        ExpiresByType image/jpeg "access plus 1 week"
        ExpiresByType image/png "access plus 1 week"
        ExpiresByType text/css "access plus 1 week"
        ExpiresByType text/javascript "access plus 1 week"
        ExpiresByType application/x-javascript "access plus 1 week"
        ExpiresByType text/xml "access plus 1 week"
    </IfModule>
</VirtualHost>

服务器管理员admin@domain.com
DocumentRoot“/usr/local/www/apache22/data/domain”
服务器名www.domain.com
ErrorLog“/var/log/apache22/domain error.log”
CustomLog“/var/log/apache22/domain access.log”组合
AllowOverride文件信息
过期于
ExpiresDefault“访问加1周”
ExpiresByType text/html“访问加1周”
ExpiresByType image/gif“访问加1周”
过期按类型图像/jpeg“访问加1周”
ExpiresByType图像/png“访问加1周”
ExpiresByType文本/css“访问加1周”
ExpiresByType文本/javascript“访问加1周”
ExpiresByType应用程序/x-javascript“访问加1周”
ExpiresByType text/xml“访问加1周”

关于可能发生的事情有什么建议吗?

看起来mod_rewrite将文件路径与URI路径弄错了。尝试将前导斜杠添加到规则的目标:

RewriteRule ^(.*)/$ /$1 [L,R=301]
但是,请记住,mod_dir和
DirectorySlash
指令(默认情况下启用)将所有请求重定向到缺少尾部斜杠的目录/文件夹,并将其重定向到带有尾部斜杠的同一URL,此规则将创建重定向循环。您可以通过在规则之前包含一个条件来规避此问题:

RewriteCond %{REQUEST_FILENAME} !-d
以下工作:

RewriteRule ^(.*)/$ http://www.domain.com/$1 [R=301,L]

谢谢你的回复。在$1之前加上斜杠并不能解决这个问题。URI字符串仍被转换为文件系统路径。@dcorsello请确保已清除缓存,因为它是301,浏览器将缓存它。找到解决方案了吗?我使用宏,因此无法硬编码域。我使用
RewriteRule^(.*)https://%{HTTP_HOST}/$1
并重定向到
https://example.com/var/www/full/path/