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

标签 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 文件中的唯一语句,也会发生同样的事情。

我的 linux 服务器和 XAMPP 服务器都出现了这个问题。在 Windows 中,URL 被重写为

http://www.domain.com/C:/xampp/htdocs/domain/widgets

如果我从规则中删除“L”,Apache 日志中会记录 301,但不会发生重写。

这是该域的 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>

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

最佳答案

看起来 mod_rewrite 将文件路径误认为是 URI 路径。尝试向规则的目标添加前导斜线:

RewriteRule ^(.*)/$ /$1 [L,R=301]

但是,请记住 mod_dir 和 DirectorySlash 指令(默认情况下打开)会将所有请求重定向到缺少尾部斜杠的目录/文件夹到具有尾部的相同 URL斜杠,此规则将创建一个重定向循环。您可以通过在规则前添加条件来规避这种情况:

RewriteCond %{REQUEST_FILENAME} !-d

关于linux - apache 重写的 URL 包含完整的 Linux 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749006/

相关文章:

php - 带有 AddHandler php5-script .php 行的 .htaccess 有什么作用?

c# - mkbundle 生成的程序(来自 C# 程序)在 Ubuntu 上运行,但在 Redhat 上不起作用

c - 物理页面的多个映射

arrays - 无法将元素添加到 bash 中的数组

android - camera.getPicture 如何使用 apache cordova 在 android 中工作?

apache - JDBC PoolingDataSource 与 PoolingDriver

linux - 不在符号链接(symbolic link)目录中符号链接(symbolic link)一个或两个文件

apache - 重定向来自特定域的所有请求

html - htaccess 重写规则。我不想全部重写 - 如何排除某些页面?

.htaccess - 在 SEF url 系统中排序评论的问题