0

I am struggling with a rewrite issue with Luracast Restler and Restler's API Explorer (a fork of Swagger UI). I have a fully working development environment running off of localhost of an OSX machine but the same files (including the .htaccess file) do not work on my pre-prod box running Ubuntu. I do have access to the httpd.conf files in both cases and have poured over them for some sort of answer. Admittedly mod-rewrite is still some form of dark-magic to me so I'd really love to get some advice from others.

I've turned on the logging in both environments with the following entry to the httpd.conf:

RewriteLog logs/rewrite.log
RewriteLogLevel 2

This produces some interesting results in pre-prod that to the trained eye might even be revealing. If I call http://website.com/api/explorer, I get:

[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

Interestingly -- and equally unexplainable to me -- is that in my local environment calling the same URL gives me no logging at all (there is a zero byte log so I believe I've configured it correctly in httpd.conf). I would have thought that in the local environment -- where Restler WORKS -- there would be a log entry for converting /api/explorer to /api/explorer/index.html.

The .htaccess file in /api/explorer looks like this:

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>

and in /api it is:

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>

UPDATE

Based on @faa suggestion I have added a base path to the .htaccess file:

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

And also changed the [QSA,L] parameters to the rules to [QSA,L,DPI]. The log now says:

(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

What perplexes me -- if I'm reading this correctly -- is that it:

  • correctly redirects from '' to 'index.html'
  • but then line 2 "try to replace prefix" seems completely redundant
  • and then WHY!#?! does it do an internal redirect with '/var/www/html/api/explorer/index.html'?

If I do a ls -l /var/www/html/api/explorer/index.html I get this:

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

Clearly this file exists and the permissions are fine for apache to serve this content.

UPDATE 2

It is also worth noting that I have Wordpress installed at the root which has a .htaccess file that is as follows (btw, this is default WP plus the popular W3 Total Cache plugin):

# 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
ken
  • 8,763
  • 11
  • 72
  • 133
  • From this `[perdir /var/www/html/api/explorer/] trying to replace prefix /var/www/html/api/explorer/ with /` I think the problem is with the prefix that is automatically added to the path of the substitution URL. Try adding this line `RewriteBase /var/www/html/api/explorer/` after `RewriteEngine On` in the first rule set. You may also try replacing `[QSA,L]` with `[QSA,L,DPI]` in the same rule-set. Just some ideas, though. – Felipe Alameda A Feb 03 '13 at 12:51
  • Thanks, @faa. I have tried your suggestions and updated above ... some changes but still not working. Does this shed any additional light on things for you? – ken Feb 03 '13 at 13:17
  • Yes, my suggestion about the prefix made things worse. Now is trying to append it to the existing one. I guess it should be only `/`. It is important to include it anyway. It looks like there is a WP install somewhere and maybe there are conflicting rules. But now I don't have a clue of what's the problem, it can be originated by many not so evident reasons. Sorry. – Felipe Alameda A Feb 03 '13 at 15:11
  • @faa, you are right about Wordpress. Is there a way to eliminate/scope the rewrite rules in /api and /api/explorer to be fully separate from those in Wordpress (setup at the root level)? – ken Feb 03 '13 at 15:45
  • Where is the WP install and is `/api/explorer` inside it's web space? – Felipe Alameda A Feb 03 '13 at 15:57
  • Yes WP is at root. /api hangs off of it. I've updated the question to include the .htaccess at root. – ken Feb 03 '13 at 16:00
  • That might explain why is everything trying to be routed to index.php. If the WP rule-set and `redirect.php` scope is affected, WP will stop to work. The standard WP rule-set is located at root inside the .htaccess file while the rule-sets in your question are in different directories, so there should be no conflicts, except, again, for the RewriteBase. Have you tried modifying the rewrite rule like this: `RewriteRule .* http://mysite.com/api/explorer/index.html [L]` ? There is no need for the regex group `()` or for the `QSA` flag as there is no query in the substitution URL, by the way. – Felipe Alameda A Feb 03 '13 at 16:23
  • I just tried that but same results. I've created a gist with all three .htaccess files: https://gist.github.com/6a3e7b52ed7e0c26f203 – ken Feb 03 '13 at 17:28

0 Answers0