No Code Attached Yet
avatar ceciogit
ceciogit
4 Oct 2020

Hello Everybody, ive searched everywhere before posting as issue here.
if i am doing it wrong (i am sorry )please be kind as i am trying to solve that since one week without success.

Inserting a RewriteRule in .htaccess custom redirects (after common exploits before J! core SEF) results in a redirect to 404 if the redirecting is not a physical file or existing path.

so for example:

Steps to reproduce the issue

in .htaccess

RewriteRule ^testslugview/(.*) index.php?option=com_content&view=$1 [L,NC] >> 404 message
RewriteRule ^testslug index.php?option=com_content&view=featured [L,NC] >> 404 message


RewriteRule ^realfilejs/(.*) templates/cassiopeia/js/$1 [L,NC] >>OK
RewriteRule ^realfilecss/(.*) templates/cassiopeia/css/$1 [L,NC] >> OK
End Custom redirects

Expected result

expected result is the featured view list of com_content or at least a component error.

Actual result

404 Page not found
Call stack

# | Function | Location
1 | () | JROOT/libraries/src/Router/Router.php:153
2 | Joomla\CMS\Router\Router->parse() | JROOT/libraries/src/Application/CMSApplication.php:1003
3 | Joomla\CMS\Application\CMSApplication->route() | JROOT/libraries/src/Application/SiteApplication.php:775
4 | Joomla\CMS\Application\SiteApplication->route() | JROOT/libraries/src/Application/SiteApplication.php:225
5 | Joomla\CMS\Application\SiteApplication->doExecute() | JROOT/libraries/src/Application/CMSApplication.php:231
6 | Joomla\CMS\Application\CMSApplication->execute() | JROOT/includes/app.php:63
7 | require_once() | JROOT/index.php:36

System information (as much as possible)

tryed almost everywhere , and those results happen only on Joomla4 beta /1 /2 /3 /4
ubuntu 18.04 with php 7.2 | 7.3 | 7.4

Additional

Thanks everybody and sorry in advance if all that mess is because of my fault.

avatar ceciogit ceciogit - open - 4 Oct 2020
avatar joomla-cms-bot joomla-cms-bot - change - 4 Oct 2020
Labels Added: ?
avatar joomla-cms-bot joomla-cms-bot - labeled - 4 Oct 2020
avatar ceciogit ceciogit - change - 4 Oct 2020
The description was changed
avatar ceciogit ceciogit - edited - 4 Oct 2020
avatar ceciogit ceciogit - change - 4 Oct 2020
The description was changed
avatar ceciogit ceciogit - edited - 4 Oct 2020
avatar ceciogit
ceciogit - comment - 4 Oct 2020

Update:
Commenting following lines of Joomla\CMS\Router the rewriteRule work again.

// Check if all parts of the URL have been parsed.
// Otherwise we have an invalid URL

151if (\strlen($uri->getPath()) > 0)
152{
153 // Commenting Here will work again (this is different from J!3.9x need to check )
154 // throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND'));
155 }

But obviously commenting like that means that every non parsed or zero length $uri->getPath() will be redirected to index.php without Firing Exception ..

it seems that in j3.9x " Router > parse() " fire the 404 exception only if all of those 3 are true :
strlen($uri->getPath()) > 0
array_key_exists('option', $vars)
ComponentHelper::getParams($vars['option'])->get('sef_advanced', 0)

i think i have lost something on the road to j4 ..

avatar ceciogit ceciogit - change - 4 Oct 2020
Title
[4.0] - Htaccess Custom RewriteRule return 404 if redirecting to something different than real files
[4.0] - Htaccess Custom RewriteRule > 404 Exception
avatar ceciogit ceciogit - edited - 4 Oct 2020
avatar ReLater
ReLater - comment - 4 Oct 2020

Please try
[R=301,L,NC]
instead of
[L,NC]

[L,NC] gives always a 404. Also on Joomla 3. That's a server thing.

avatar ceciogit
ceciogit - comment - 4 Oct 2020

[R=301,L,NC]

Hello @ReLater Thank you for your answer , but R=301 will perform a 301 Redirect, the url will not be rewrited but truly Redirected

[L,NC] gives always a 404. Also on Joomla 3. That's a server thing.
Sorry, no i have 25 websites with [L,NC] on production J3 Websites.. and no 404 Exceptions

avatar ceciogit
ceciogit - comment - 4 Oct 2020

Update:
I have modified the Router.php to fullfill my needs, but im sure that my code is ugly as hell. (i will post it shameless down below)

anyway ive understand why "custom rewriting rules to index.php" on htaccess is not working anymore in J4
in the old Router (J3.x) as already stated above the parse function check for $vars['option'] before triggering an Exception to 404.
this is not the case in J4! because the exception is fired as soon at the code understand that length of $uri->getPath()) > 0) is not empty/zero. I have tried to override/bypass/implements in the component routing but without success as because the stack interrupt the routing (firing exception) way before reaching the custom routing rules on component.

this is the ugly code ive wrote to bypass the problem for the moment:

if (\strlen($uri->getPath()) > 0 && $uri->getVar('query') == NULL)
{
$opt = Factory::getApplication()->input->getString('option',false);
if(!$opt) {
throw new RouteNotFoundException(Text::_('JERROR_PAGE_NOT_FOUND'));
}
}

and that said my skills regarding joomla code are over ?

Please, someone with more skills is needed here because i still not understand if this is an issue or if its me that do not code properly on joomla.

avatar ceciogit
ceciogit - comment - 9 Dec 2020

Hello Folks!
someone with more skills can give a check on this and clarify if this is an issue?

avatar HLeithner
HLeithner - comment - 9 Dec 2020

@ceciogit can you please post a var_dump of $uri before it throws the exception?

avatar ceciogit
ceciogit - comment - 9 Dec 2020

Hi @HLeithner , yep!

Consider that in htaccess " ^seftext/sefvar index.php?option=com_content&view=featured "

object(Joomla\CMS\Uri\Uri)#672 (10) { ["uri":protected]=> string(53) "http://domain/seftext/sefvar" ["scheme":protected]=> string(4) "http" ["host":protected]=> string(19) "domain.com" ["port":protected]=> NULL ["user":protected]=> NULL ["pass":protected]=> NULL ["path":protected]=> string(18) "seftext/sefvar" ["query":protected]=> NULL ["fragment":protected]=> NULL ["vars":protected]=> array(3) { ["option"]=> string(11) "com_content" ["view"]=> string(8) "featured" ["Itemid"]=> int(101) } }

avatar ceciogit
ceciogit - comment - 23 Jan 2021

Hello Coders..
ive maked many tests and all .htaccess files i have in 35 J!3.9x websites does not work with currently J!4 (tested up to beta 6) Route.php lib.
someone can help us flagging this as Issue?

avatar ceciogit
ceciogit - comment - 23 Feb 2022

1 year and still open?
can someone take a look at it?

avatar HLeithner HLeithner - change - 23 Feb 2022
The description was changed
avatar HLeithner HLeithner - edited - 23 Feb 2022
avatar HLeithner HLeithner - change - 23 Feb 2022
The description was changed
avatar HLeithner HLeithner - edited - 23 Feb 2022
avatar HLeithner
HLeithner - comment - 23 Feb 2022

@Hackwar is this expected behavior?

avatar brianteeman
brianteeman - comment - 23 Feb 2022

I tried it in j3 and it didnt work there either

avatar brianteeman
brianteeman - comment - 23 Feb 2022

When following the docs and using R=301,L then it works

joomla-cms/htaccess.txt

Lines 70 to 76 in 52553bf

## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects

avatar HLeithner
HLeithner - comment - 24 Feb 2022

I tested it in 3 and it worked... A redirect is something different then then the requested rule.

So the question is if j4 can be changed so it works.

avatar Hackwar
Hackwar - comment - 24 Feb 2022

@Hackwar is this expected behavior?

Short:
Yes, this is expected behavior.

Long:
If we comment that line in the router, we re-introduce the problem that invalid URLs still give a 200 code. Example would be: /this-is-a-valid-url is okay, but with this change /this-is-a-valid-url/something-else would still return the same page and give a 200 code. That is why we can't simply comment that. Since it seems as if this redirect messes up the environment variables, this seems not be possible. The proper way would be to redirect to the right SEF URL...

avatar HLeithner HLeithner - close - 24 Feb 2022
avatar HLeithner
HLeithner - comment - 24 Feb 2022

ok, sound reasonable. I looked at the relevant code line and saw that all ParseRules are processed before this check so it would be possible to write a plugin and clean the path before it reaches this check.

@ceciogit is this enough for you?

the system plugin could be something like:

<?php

class plgSystemSEFHACK extends \Joomla\CMS\Plugin\CMSPlugin
{
    protected $app;

    public function onAfterInitialise()
    {

        // this rule is only relevant for the frontend
        if ($this->app->isClient('site')) {
            $router = $app->getRouter();
            $router->attachParseRule(array($this, 'parseRule'), \Joomla\CMS\Router\Router::PROCESS_AFTER);
        }
    }

    public function parseRule(&$router, \Joomla\CMS\Uri\Uri &$uri)
    {
        if (\strlen($uri->getPath()) > 0)
        {
            $uri->setPath('');
        }
    }
}

That's only a PoC and hasn't been tested also I can't say what side effects this would generate. Maybe you should check if the option is com_content or the path is the same as in your htaccess file.

I think that should solve your problem and I close this ticket as expected behavior including a custom solution.

avatar HLeithner HLeithner - change - 24 Feb 2022
Status New Closed
Closed_Date 0000-00-00 00:00:00 2022-02-24 09:38:06
Closed_By HLeithner
Labels Added: No Code Attached Yet
Removed: ?
avatar ceciogit
ceciogit - comment - 16 May 2022

Sorry for being Late on answering you guys, but had lot issue in those months.
Thank you @HLeithner @brianteeman and @Hackwar

Add a Comment

Login with GitHub to post a comment