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:

// 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");

To see if redirection even works, I tried this piece which gives me a redirect error.

Redirect 301 "/" "https://example.com/?pg=pgnumberhere"
  • Does your scripthere.php work if you call it directly (is getLatestSlug() 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?)
    – MrWhite
    Commented Oct 11, 2023 at 8:58
  • I apologize again I'm very inexperienced with coding and the results I tried were a combination of Google and ChatGPT. I'm going for a redirect because of the comment section being unique for each page - The comments in example.com aren't comic page specific, so my idea is to disable the form there and try to redirect users to /?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 what getLatestSlug() really represent.
    – redmoon321
    Commented Oct 11, 2023 at 9:21
  • The PHP code you posted is far from complete. The key is what 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?
    – MrWhite
    Commented Oct 11, 2023 at 15:09
  • There's this let pg = Number(findGetParameter("pg")); in the code and const maxpg = 123;
    – redmoon321
    Commented Oct 12, 2023 at 0:17
  • That doesn't tell you what the latest "pagenumber" is. Unless that is 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?
    – MrWhite
    Commented Oct 12, 2023 at 9:06

2 Answers 2


A redirect rule needs to know what it is redirecting to when it is applied. This is not known at config time. I don't think rewrites are the way to solve the problem.

Clearly you have management of metadata (the linkage between the pagenumber and the content files) implemented for adding and retrieving content. Further, you have PHP available and a mechanism for determining the default content algorithmically. Hence you could....

Present a PHP script at https://example.com/index.php which loads the intended content at that URL.

Present a PHP script at https://example.com/index.php which redirects to a static file containing the intended content.

Map the PHP script as a 404 handler (and add logic to handle cases where the request is for something other than https://example.com/).

Modfy the deployment method to replace /index/html with a symlink to the intended content (note you'll probably have to explicitly allow followSymlinks).


I found a working solution that doesn't cause redirect errors or pages being stuck or constantly refreshing the page. I have to manually edit the const maxPageNumber = however.

    // Define the latest page number and maximum page number.
    let latestPageNumber = Number(findGetParameter("pg"));
    const maxPageNumber = 123; // Replace with the actual maximum page number.

    // Ensure latestPageNumber is within the valid range.
    if (isNaN(latestPageNumber) || latestPageNumber < 1 || latestPageNumber > maxPageNumber) {
        latestPageNumber = maxPageNumber; // Use the maximum if the value is invalid.

    // Construct the target URL.
    const targetURL = `https://example.com/?pg=${latestPageNumber}#showComic`;

    // Perform the dynamic redirection.
    window.location.href = targetURL;

    // Function to extract GET parameters from the URL.
    function findGetParameter(parameterName) {
        let result = null,
            tmp = [];
            .forEach(function (item) {
                tmp = item.split("=");
                if (tmp[0] === parameterName) result = decodeURIComponent(tmp[1]);
        return result;
  • PHP knows what the page number should be. Get it to write the value into the Javascript content (or use one of the methods in other answers)
    – symcbean
    Commented Oct 12, 2023 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.