重写和重建者

时间:2013-02-03 01:24:14

标签: mod-rewrite restler swagger

我正在努力解决Luracast Restler和Restler的API ExplorerSwagger UI的一个分支)的重写问题。我有一个完全工作的开发环境运行OSX机器的localhost,但相同的文件(包括.htaccess文件)不能在我运行Ubuntu的pre-prod盒子上运行。在这两种情况下我都可以访问httpd.conf文件并且已经倾倒了它们以获得某种答案。不可否认,mod-rewrite仍然是某种形式的黑魔法,所以我真的很想从别人那里得到一些建议。

我已使用httpd.conf中的以下条目打开了两个环境中的日志记录:

RewriteLog logs/rewrite.log
RewriteLogLevel 2

这会在前期产生一些有趣的结果,即受过训练的眼睛甚至可能会显露出来。如果我致电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的日志条目。

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

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

0 个答案:

没有答案