No Code Attached Yet
avatar vicn1222
vicn1222
3 Oct 2022

Steps to reproduce the issue

I copy htaccess.txt supplied by Joomla into .htaccess, then add line between
"## Begin - Custom redirects" and "## End - Custom redirects", as below

RewriteRule ^history/([^-]*)-?(.*).htm$ index.php?option=com_content&view=article&id=52&q=$1&page=$2 [NC,L]

My url is like
http://localhost/history/32456.htm, this return 404 "Page not found".

My investigation found it is in file libraries/src/Router/Router.php. If I comment out
throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND'));

The ReWriteRule works. But then I never get "Page Not Found", even if a page really doesn't exists.

public function parse(&$uri, $setVars = false)
    {
        // Do the preprocess stage of the URL parse process
        $this->processParseRules($uri, self::PROCESS_BEFORE);

        // Do the main stage of the URL parse process
        $this->processParseRules($uri);

        // Do the postprocess stage of the URL parse process
        $this->processParseRules($uri, self::PROCESS_AFTER);
/*
        var_dump( $uri );
        var_dump( $uri->getPath() );
        die();
 */
        // Check if all parts of the URL have been parsed.
        // Otherwise we have an invalid URL
        if (\strlen($uri->getPath()) > 0) {
            //throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND'));
        }

        if ($setVars) {
            $this->setVars($uri->getQuery(true));

            return $this->getVars();
        }

        return $uri->getQuery(true);
}

Votes

# of Users Experiencing Issue
1/1
Average Importance Score
5.00

avatar vicn1222 vicn1222 - open - 3 Oct 2022
avatar vicn1222 vicn1222 - change - 3 Oct 2022
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 3 Oct 2022
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 3 Oct 2022
avatar richard67
richard67 - comment - 5 Oct 2022

Your rewrite rule expects the part of the URL after the "history/" to have 2 parts, separated by a dash ("-"). The first part before the dash is $1 and the second part is $2 in the URL after rewrite. The dash is mandatory in the old URL in your rewrite rule, so if the URl has no dash at that place, the rewrite will not take place.

But the URL http://localhost/history/32456.htm does not have any dash in "32456.htm".

Are you really sure that your rewrite rule is right an working (without Joomla)?

avatar vicn1222
vicn1222 - comment - 6 Oct 2022

Dash is optional, not mandatory (notice there is a question mark before $2). If $2 is not provided, dash becomes optional. This has been working in Joomla 3 without any issue.

I also split the rule into two rules, as bellow, both rules throw the exception in Joomla 4. Doing so in Joomla 3, both continue to work.

RewriteRule ^history/([^-]*).htm$ index.php?option=com_content&view=article&id=52&q=$1&page=0 [NC,L]
RewriteRule ^history/([^-]*)-(.*).htm$ index.php?option=com_content&view=article&id=52&q=$1&page=$2 [NC,L]

To prove "-" is optional when $2 is not provided, I try same rules without going through joomla, as bellow

RewriteRule ^history/([^-]*)-?(.*).htm$ /test.php?q=$1&page=0 [NC,L]

I use 3 urls to test, as bellow

url1: http://localhost/history/32456.htm
url2: http://localhost/history/32456-.htm
url3: http://localhost/history/32456-2.htm

They all pass correct parameters into test.php


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/38899.

avatar vicn1222
vicn1222 - comment - 6 Oct 2022

There is a typo. The test .htaccess should be

RewriteRule ^history/([^-]*)-?(.*).htm$ /test.php?q=$1&page=$2 [NC,L]

This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/38899.
avatar vicn1222
vicn1222 - comment - 11 Oct 2022

Hmmm, don't know why my previous followup got lost. I will repost my fixes again.

Bellow is my fixes. Someone please review the codes and change as necessary? Thanks!

    public function parse(&$uri, $setVars = false)
    {
        // Do the preprocess stage of the URL parse process
        $this->processParseRules($uri, self::PROCESS_BEFORE);

        // Do the main stage of the URL parse process
        $this->processParseRules($uri);

        // Do the postprocess stage of the URL parse process
        $this->processParseRules($uri, self::PROCESS_AFTER);

        // Check if all parts of the URL have been parsed.
        // Otherwise we have an invalid URL
        if ( \strlen( $uri->getPath() ) > 0 ) {
          $vars = $uri->getQuery(true);
          if ( array_key_exists( "Itemid", $vars ) ) {
            $table = \JTable::getInstance( "Menu" );
            $table->load( [ "id" => $vars[ "Itemid" ] ] );

            // trying to redirect to home page when getPath() is not empty, page doesn't exists!
            if ( $table->home == 1 ) {
              throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND'));
            }
          }

        }

        if ($setVars) {
            $this->setVars($uri->getQuery(true));

            return $this->getVars();
        }

        return $uri->getQuery(true);
    }

This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/38899.
avatar Quy Quy - change - 22 Feb 2023
Status New Closed
Closed_Date 0000-00-00 00:00:00 2023-02-22 22:01:27
Closed_By Quy
avatar Quy
Quy - comment - 22 Feb 2023

Closing in favor of #39916 which has additional info. Thanks.

avatar Quy Quy - close - 22 Feb 2023

Add a Comment

Login with GitHub to post a comment