Skip to content

Commit 2cf5247

Browse files
committed
Fixed URLs in .htaccess when baseUrl points to a subdirectory.
Fixed "#" in redirection targets messing up target file validation.
1 parent d379bdf commit 2cf5247

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

examples/apache/content/404.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ page.isSpecial = true
77

88
Looks like you've taken a wrong turn somewhere!
99

10-
[Go back to the front page](/)
10+
[Go back to the front page]({{ / }})

examples/apache/content/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"/view.php?page=flowers", -- Old link.
1010
"/bad-link/", -- 404 error.
1111
} *}}
12-
- [{{ prettyUrl(it) }}]({{ it }})
12+
- [{{ prettyUrl(it) }}]({{ url(it) }})
1313
{{ end }}

examples/apache/content/plants.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@ Imagine some nice plants here.
1616
,,,\|/|,,,,,
1717
</pre>
1818

19-
[Go back to the front page](/)
19+
[Go back to the front page]({{ / }})

src/app.lua2p

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ local function setup()
8080
baseUrl = "http://:host:/",
8181
languageCode = "en",
8282

83-
ignoreFiles = {"%.tmp$"},
83+
ignoreFiles = {"%.lnk$","%.tmp$","^Thumbs%.db$"},
8484
ignoreFolders = {"^%."},
8585
}
8686
]=], {
@@ -1651,7 +1651,7 @@ local function buildWebsite()
16511651

16521652
-- Validate targets.
16531653
for url, targetUrl in pairsSorted(allRedirections) do
1654-
if targetUrl:find"^/" and not site._writtenOutputUrls[targetUrl] then
1654+
if targetUrl:find"^/" and not site._writtenOutputUrls[targetUrl:gsub("[?#].*", "")] then
16551655
errorNoPos("Missing target page for redirection: %s -> %s", url, targetUrl)
16561656
end
16571657
end
@@ -1700,17 +1700,18 @@ local function buildWebsite()
17001700
if next(site._htaErrors) then
17011701
local b = newStringBuilder()
17021702

1703-
for errCode, target in pairsSorted(site._htaErrors) do
1703+
for errCode, targetDoc in pairsSorted(site._htaErrors) do
17041704
-- The target may be a URL or HTML code.
1705-
--
1706-
-- @Incomplete: Does Apache rewrite URLs to error documents? I don't think v2.2 did, but maybe
1707-
-- that has changed. If so, we should test for multiple file locations. :ApacheErrorDocumentFile
1708-
--
1709-
if target:find"^/%S*$" and not isFile(DIR_OUTPUT..target) then
1710-
errorNoPos("Missing target page for error %d document (in output folder): %s%s", errCode, DIR_OUTPUT, target)
1705+
if targetDoc:find"^/%S*$" then
1706+
-- @Incomplete: Does Apache rewrite URLs to error documents? I don't think v2.2 did, but maybe
1707+
-- that has changed. If so, we should test for multiple file locations. :ApacheErrorDocumentFile
1708+
if not isFile(DIR_OUTPUT..targetDoc:gsub("[?#].*", "")) then
1709+
errorNoPos("%s: Missing target page for error %d document (in output folder): %s%s", maybeFullPath"config.lua", errCode, DIR_OUTPUT, targetDoc)
1710+
end
1711+
targetDoc = fixRelativeUrl(targetDoc)
17111712
end
17121713

1713-
b("ErrorDocument %d %s\n", errCode, target)
1714+
b("ErrorDocument %d %s\n", errCode, targetDoc)
17141715
end
17151716

17161717
contents = F("%s\n%s", contents, b())
@@ -1751,24 +1752,33 @@ local function buildWebsite()
17511752
b("\t# Redirect moved resources.\n")
17521753

17531754
for url, targetUrl in pairsSorted(site._writtenRedirects) do
1755+
-- Make absolute target URLs relative if possible.
17541756
if targetUrl:sub(1, #site.baseUrl.v) == site.baseUrl.v then
17551757
targetUrl = targetUrl:sub(#site.baseUrl.v) -- Note: We keep the initial '/'.
17561758
end
17571759

1760+
url = fixRelativeUrl(url)
1761+
targetUrl = fixRelativeUrl(targetUrl)
1762+
17581763
b('\tRewriteCond %%{REQUEST_URI} "=%s"\n', url)
17591764
b('\tRewriteRule .* "%s" [R=301,L]\n', escapeRuleSub(targetUrl))
17601765
end
17611766

17621767
for url, targetUrl in pairsSorted(site._unwrittenRedirects) do
1768+
-- @Copypaste from above.
1769+
1770+
-- Make absolute target URLs relative if possible.
17631771
if targetUrl:sub(1, #site.baseUrl.v) == site.baseUrl.v then
17641772
targetUrl = targetUrl:sub(#site.baseUrl.v) -- Note: We keep the initial '/'.
17651773
end
17661774

17671775
local slug, query = url:match"^(.-)%?(.*)$"
17681776
if not slug then slug = url end
17691777

1770-
b('\tRewriteCond %%{REQUEST_URI} "=%s"\n', slug)
1778+
slug = fixRelativeUrl(slug)
1779+
targetUrl = fixRelativeUrl(targetUrl)
17711780

1781+
b('\tRewriteCond %%{REQUEST_URI} "=%s"\n', slug)
17721782
if query then
17731783
b('\tRewriteCond %%{QUERY_STRING} "=%s"\n', query)
17741784
end
@@ -1796,6 +1806,7 @@ local function buildWebsite()
17961806
b("\tRewriteCond %{ENV:REDIRECT_STATUS} ^$\n")
17971807

17981808
for i, urlPrefix in ipairs(htaDenyAccess) do
1809+
urlPrefix = fixRelativeUrl(urlPrefix)
17991810
b(
18001811
'\tRewriteCond %%{REQUEST_URI} "^%s"%s\n',
18011812
escapeCondPat(urlPrefix),
@@ -1808,16 +1819,20 @@ local function buildWebsite()
18081819
end
18091820

18101821
if htaPrettyUrlDir ~= "" then
1822+
if not isDirectory(DIR_OUTPUT.."/"..htaPrettyUrlDir) then
1823+
errorNoPos("%s: config.htaccess.XXX_prettyUrlDirectory points to a missing directory (in output folder): %s/%s", maybeFullPath"config.lua", DIR_OUTPUT, htaPrettyUrlDir)
1824+
end
1825+
18111826
b('\t# Point to "%s" directory.\n', htaPrettyUrlDir)
18121827
b("\tRewriteCond %{REQUEST_FILENAME} !-f\n")
1813-
b('\tRewriteCond "%%{DOCUMENT_ROOT}/%s/%%{REQUEST_URI}" -f\n', escapeTestStr(htaPrettyUrlDir))
1814-
b('\tRewriteRule .* "/%s/$0" [L]\n', escapeRuleSub(htaPrettyUrlDir))
1828+
b('\tRewriteCond "%%{DOCUMENT_ROOT}%s%s/%%{REQUEST_URI}" -f\n', fixRelativeUrl"/", escapeTestStr(htaPrettyUrlDir))
1829+
b('\tRewriteRule .* "%s%s/$0" [L]\n', fixRelativeUrl"/", escapeRuleSub(htaPrettyUrlDir))
18151830
b("\n")
18161831

18171832
b("\tRewriteCond %{REQUEST_FILENAME} !-f\n")
18181833
b("\tRewriteCond %{REQUEST_URI} !^/$\n")
1819-
b('\tRewriteCond "%%{DOCUMENT_ROOT}/%s/%%{REQUEST_URI}/" -d\n', escapeTestStr(htaPrettyUrlDir))
1820-
b('\tRewriteRule .* "/%s/$0/" [L]\n', escapeRuleSub(htaPrettyUrlDir))
1834+
b('\tRewriteCond "%%{DOCUMENT_ROOT}%s%s/%%{REQUEST_URI}/" -d\n', fixRelativeUrl"/", escapeTestStr(htaPrettyUrlDir))
1835+
b('\tRewriteRule .* "%s%s/$0/" [L]\n', fixRelativeUrl"/", escapeRuleSub(htaPrettyUrlDir))
18211836
b("\n")
18221837
end
18231838

@@ -1830,8 +1845,9 @@ local function buildWebsite()
18301845

18311846
local directives = b()
18321847

1833-
local count1 ; contents, count1 = gsubPlainSub(contents, "#[ \t]*:webgen%.rewriting:[ \t]*\n", directives)
1834-
local count2 ; contents, count2 = gsubPlainSub(contents, "#[ \t]*:webgen%.rewriting:[ \t]*$", directives)
1848+
local count1, count2
1849+
contents, count1 = gsubPlainSub(contents, "#[ \t]*:webgen%.rewriting:[ \t]*\n", directives)
1850+
contents, count2 = gsubPlainSub(contents, "#[ \t]*:webgen%.rewriting:[ \t]*$", directives)
18351851
if count1+count2 == 0 then
18361852
contents = F("%s\n%s", contents, directives)
18371853
end

src/functions.lua2p

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
pairsSorted
5252
parseAndRunTemplate
5353
pathToSitePath, sitePathToPath
54-
percentEncode, partialEncodeUrl, fixRelativeUrlAndEncode, toAbsoluteAndEncodedUrl, urlize, urlToPrettyText
54+
percentEncode, partialEncodeUrl, fixRelativeUrl, fixRelativeUrlAndEncode, toAbsoluteAndEncodedUrl, urlize, urlToPrettyText
5555
printNoLog, printfNoLog, log, print, printOnce, printf, printfOnce, timestampPrint, timestampPrintOnce, timestampPrintVerbose, timestampPrintError, timestampPrintWarning, timestampPrintWarningOnce, printObject, logErrorTraceback
5656
pushContext, assertContext, getContext, isContext
5757
removeItem
@@ -1350,10 +1350,14 @@ do
13501350
return (urlLib.escape(url):gsub("%%%x%x", URI_PERCENT_CODES_TO_NOT_ENCODE))
13511351
end
13521352

1353+
function _G.fixRelativeUrl(url)
1354+
url = url:gsub("^/%f[^/]", (site.baseUrl.v:gsub("^%w+://[^/]+", ""))) -- @Speed
1355+
return url
1356+
end
1357+
13531358
function _G.fixRelativeUrlAndEncode(url)
13541359
!ARGS "url:string"
1355-
url = url:gsub("^/%f[^/]", (site.baseUrl.v:gsub("^%w+://[^/]+", ""))) -- @Speed
1356-
return (partialEncodeUrl(url))
1360+
return (partialEncodeUrl(fixRelativeUrl(url)))
13571361
end
13581362

13591363
function _G.toAbsoluteAndEncodedUrl(url)

0 commit comments

Comments
 (0)