I'm inexperienced with coding.
I have a comic that creates new pages as /?pg=pagenumber
I want visitors to be redirected to the latest page. So when they type: https://example.com
they get redirected to https://example.com/?pg=latestpagenumber
The latestpagenumber changes every time I add a new page.
I'm using .htaccess to edit this rule. I tried a couple of edits, but they do not work. I have another rewrite rule that works (removing index.html).
I have tried:
RewriteEngine On
RewriteRule ^example.com/(\\d+)/?$ /example.com/?pg=$1 \[L,R=301\]\
RewriteEngine On
RewriteRule ^example.com/(.*)$ /example.com/$1 [L,R=301]
RewriteEngine on
RewriteCond %{THE_REQUEST} /https://example\.com\ [NC]
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /https://example.com/?pg=$1 [L]
This that contains a script:
RewriteEngine On
RewriteRule ^example.com/?$ scripthere.php [L]
The php script contains:
<?php
// Code to determine the most recent slug (e.g., from a database or a file).
$latestSlug = getLatestSlug();
// Redirect to the most recent page using a 301 (Permanent) redirect.
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://example.com/$latestSlug");
exit();
?>
To see if redirection even works, I tried this piece which gives me a redirect error.
Redirect 301 "/" "https://example.com/?pg=pgnumberhere"
scripthere.php
work if you call it directly (isgetLatestSlug()
returning the correct URL-part?)? But don't use a 301 (permanent) redirect, use a 302 (temporary) redirect - if you redirect at all. (Although I'd question whether a redirect from root would be advisable? What about a URL like/latest
?)/?pg=pagenumber
when going to domain. I'm using a template that generates pages with js and all I do is plug and follow instructions. I tried the php code by uploading the file to root and linking accordingly. I don't know whatgetLatestSlug()
really represent.getLatestSlug()
does (which is arguably named incorrectly for your use case) - and that is not implemented here. How do you determine what the latest "pagenumber" is?let pg = Number(findGetParameter("pg"));
in the code andconst maxpg = 123;
maxpg
and you have to update the script each time you add a new comic?! How do you know what the latest "pagenumber" (comic) is? Is this stored or calculated in some way? Unless you can "calculate/lookup" what the latest "pagenumber" is then you can't simply redirect to whatever the latest "pagenumber" is, without hardcoding the latest pagenumber each time you add a comic. Is that what you do? And if so where/how?