0

我正在努力解决Luracast Restler和 Restler 的API ExplorerSwagger UI的一个分支)的重写问题。我有一个在 OSX 机器的本地主机上运行的完全正常工作的开发环境,但相同的文件(包括 .htaccess 文件)在我运行 Ubuntu 的预生产盒上不起作用。在这两种情况下,我都可以访问 httpd.conf 文件,并倾注于它们以寻求某种答案。诚然,mod-rewrite 对我来说仍然是某种形式的黑暗魔法,所以我真的很想从其他人那里得到一些建议。

我已经在 httpd.conf 的以下条目中打开了两种环境中的日志记录:

RewriteLog logs/rewrite.log
RewriteLogLevel 2

这会在 pre-prod 中产生一些有趣的结果,这些结果对于受过训练的眼睛甚至可能会有所启发。如果我打电话http://website.com/api/explorer,我会得到:

[perdir /var/www/html/api/] rewrite 'resources.json' -> 'index.php'
[perdir /var/www/html/api/] trying to replace prefix /var/www/html/api/ with /
[perdir /var/www/html/api/] internal redirect with /index.php [INTERNAL REDIRECT]
[perdir /var/www/html/] pass through /var/www/html/index.php
[perdir /var/www/html/api/explorer/] rewrite '' -> 'index.html'
[perdir /var/www/html/api/explorer/] trying to replace prefix /var/www/html/api/explorer/ with /
[perdir /var/www/html/api/explorer/] internal redirect with /index.html [INTERNAL REDIRECT]
[perdir /var/www/html/] rewrite 'index.html' -> '/index.php'
[perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
[perdir /var/www/html/] internal redirect with /index.php [INTERNAL REDIRECT]
[perdir /var/www/html/] pass through /var/www/html/index.php
[perdir /var/www/html/] rewrite 'wp-content/themes/pagelines/pagelines-compiled-css-1_1356593315/' -> '/index.php'
[perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
[perdir /var/www/html/] internal 1168/initial/redir#1] (1) [perdir /var/www/html/] pass through /var/www/html/index.php

有趣的是——对我来说同样无法解释——在我的本地环境中调用相同的 URL 根本不会给我任何日志记录(有一个零字节日志,所以我相信我已经在 httpd.conf 中正确配置了它)。我原以为在本地环境中——Restler 工作的地方——会有一个日志条目用于转换/api/explorer/api/explorer/index.html.

中的 .htaccess 文件/api/explorer如下所示:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ index.html [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>

其中/api是:

DirectoryIndex index.php
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^$ index.php [QSA,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

更新

根据@faa 的建议,我添加了 .htaccess 文件的基本路径:

RewriteBase /var/www/html/api/explorer/ 

并且还将[QSA,L]规则的参数更改为[QSA,L,DPI]. 日志现在说:

(2) [perdir /var/www/html/api/explorer/] rewrite '' -> 'index.html'
(2) [perdir /var/www/html/api/explorer/] trying to replace prefix /var/www/html/api/explorer/ with /var/www/html/api/explorer/
(1) [perdir /var/www/html/api/explorer/] internal redirect with /var/www/html/api/explorer/index.html [INTERNAL REDIRECT]
(2) [perdir /var/www/html/] rewrite 'var/www/html/api/explorer/index.html' -> '/index.php'
(2) [perdir /var/www/html/] trying to replace prefix /var/www/html/ with /
(1) [perdir /var/www/html/] internal redirect with /index.php [INTERNAL REDIRECT]
(1) [perdir /var/www/html/] pass through /var/www/html/index.php

令我困惑的是——如果我没看错的话——是:

  • 正确地从 '' 重定向到 'index.html'
  • 但随后第 2 行“尝试替换前缀”似乎完全多余
  • 然后为什么!#?!它是否使用“/var/www/html/api/explorer/index.html”进行内部重定向?

如果我这样做,ls -l /var/www/html/api/explorer/index.html我会得到:

-rw-rw-r-- 1 ec2-user apache 3292 Jan 23 09:00 /var/www/html/api/explorer/index.html

显然这个文件存在,并且 apache 提供这个内容的权限很好。

更新 2

还值得注意的是,我在根目录安装了 Wordpress,它有一个 .htaccess 文件,如下所示(顺便说一句,这是默认的 WP 加上流行的 W3 Total Cache 插件):

# BEGIN W3TC Browser Cache
<IfModule mod_deflate.c>
    <IfModule mod_setenvif.c>
        BrowserMatch ^Mozilla/4 gzip-only-text/html
        BrowserMatch ^Mozilla/4\.0[678] no-gzip
        BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html
    </IfModule>
    <IfModule mod_headers.c>
        Header append Vary User-Agent env=!dont-vary
    </IfModule>
    <IfModule mod_filter.c>
        AddOutputFilterByType DEFLATE text/css application/x-javascript text/x-component text/html text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon
    </IfModule>
</IfModule>
<FilesMatch "\.(css|js|htc|CSS|JS|HTC)$">
    <IfModule mod_headers.c>
        Header set Pragma "public"
        Header set Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"
    </IfModule>
    FileETag MTime Size
    <IfModule mod_headers.c>
         Header set X-Powered-By "W3 Total Cache/0.9.2.4"
    </IfModule>
</FilesMatch>
<FilesMatch "\.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml|HTML|HTM|RTF|RTX|SVG|SVGZ|TXT|XSD|XSL|XML)$">
    <IfModule mod_headers.c>
        Header set Pragma "public"
        Header set Cache-Control "max-age=3600, public, must-revalidate, proxy-revalidate"
    </IfModule>
    FileETag MTime Size
    <IfModule mod_headers.c>
         Header set X-Powered-By "W3 Total Cache/0.9.2.4"
    </IfModule>
</FilesMatch>
<FilesMatch "\.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip|ASF|ASX|WAX|WMV|WMX|AVI|BMP|CLASS|DIVX|DOC|DOCX|EOT|EXE|GIF|GZ|GZIP|ICO|JPG|JPEG|JPE|MDB|MID|MIDI|MOV|QT|MP3|M4A|MP4|M4V|MPEG|MPG|MPE|MPP|OTF|ODB|ODC|ODF|ODG|ODP|ODS|ODT|OGG|PDF|PNG|POT|PPS|PPT|PPTX|RA|RAM|SVG|SVGZ|SWF|TAR|TIF|TIFF|TTF|TTC|WAV|WMA|WRI|XLA|XLS|XLSX|XLT|XLW|ZIP)$">
    <IfModule mod_headers.c>
        Header set Pragma "public"
        Header set Cache-Control "max-age=31536000, public, must-revalidate, proxy-revalidate"
    </IfModule>
    FileETag MTime Size
    <IfModule mod_headers.c>
         Header set X-Powered-By "W3 Total Cache/0.9.2.4"
    </IfModule>
</FilesMatch>
# END W3TC Browser Cache
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
4

0 回答 0