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); }
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
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
There is a typo. The test .htaccess should be
RewriteRule ^history/([^-]*)-?(.*).htm$ /test.php?q=$1&page=$2 [NC,L]
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); }
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2023-02-22 22:01:27 |
Closed_By | ⇒ | Quy |
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)?