No Code Attached Yet bug
avatar vicn1222
vicn1222
12 Oct 2023

Currently Joomla 4 is not parsing url rewrite in .htaccess, thus requiring to write custom router to parse the url. This proposed change is to fix the url generation so that there is no need to write custom url parser/router.

Steps to reproduce the issue

  1. create an article "foo-article" and a menu "foo-menu". The "foo-menu" will accept query string, such as q=xyz.
  2. The browser url will look like http://localhost/foo-menu/xyz.html
  3. In .htaccess, add re-write rule below

RewriteRule ^foo-menu/(.*).html$ index.php/foo-menu?q=$1 [NC,L]

  1. The browser url http://localhost/foo-menu/xyz.html thus becomes http://localhost/foo-menu?q=xyz according to the rewrite rule.

  2. However, current router is to parse the browser url of http://localhost/foo-menu/xyz.html, not the rewrite url of http://localhost/foo-menu?q=xyz. This then forces users to create custom router to parse the url.

Expected result

Expect to obey .htaccess rewrite rule when generating the url.

Actual result

.htaccess rewrite rule is ignored.

System information (as much as possible)

Joomla! 4.3.4
Red Hat Apache server

Additional comments

Proposed fix is at libraries/src/Uri/Uri.php when building $theURI in function getInstance as below
(I can not add file "Uri.php", I rename it to "Uri.txt").

public static function getInstance($uri = 'SERVER')
{
    if (empty(static::$instances[$uri])) {
        // Are we obtaining the URI from the server?
        if ($uri === 'SERVER') {
            // Determine if the request was over SSL (HTTPS).
            if (isset($_SERVER['HTTPS']) && !empty($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) !== 'off')) {
                $https = 's://';
            } elseif (
                (isset($_SERVER['HTTP_X_FORWARDED_PROTO'])
                && !empty($_SERVER['HTTP_X_FORWARDED_PROTO'])
                && (strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) !== 'http'))
            ) {
                $https = 's://';
            } else {
                $https = '://';
            }

            /*
             * Since we are assigning the URI from the server variables, we first need
             * to determine if we are running on apache or IIS.  If PHP_SELF and REQUEST_URI
             * are present, we will assume we are running on apache.
             */

            if (!empty($_SERVER['PHP_SELF']) && !empty($_SERVER['PATH_INFO']) && !empty($_SERVER['QUERY_STRING'])) {

                $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['PATH_INFO'] . '?' . $_SERVER['QUERY_STRING'];;

            } else if (!empty($_SERVER['PHP_SELF']) && !empty($_SERVER['REQUEST_URI'])) {
                // To build the entire URI we need to prepend the protocol, and the http host
                // to the URI string.
                $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
            } else {
                /*
                 * Since we do not have REQUEST_URI to work with, we will assume we are
                 * running on IIS and will therefore need to work some magic with the SCRIPT_NAME and
                 * QUERY_STRING environment variables.
                 *
                 * IIS uses the SCRIPT_NAME variable instead of a REQUEST_URI variable... thanks, MS
                 */
                $theURI = 'http' . $https . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];

                // If the query string exists append it to the URI string
                if (isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) {
                    $theURI .= '?' . $_SERVER['QUERY_STRING'];
                }
            }

            // Extra cleanup to remove invalid chars in the URL to prevent injections through the Host header
            $theURI = str_replace(["'", '"', '<', '>'], ['%27', '%22', '%3C', '%3E'], $theURI);
        } else {
            // We were given a URI
            $theURI = $uri;
        }

        static::$instances[$uri] = new static($theURI);
    }

    return static::$instances[$uri];
}

Uri.txt

Votes

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

avatar vicn1222 vicn1222 - open - 12 Oct 2023
avatar vicn1222 vicn1222 - change - 12 Oct 2023
Labels Removed: ?
avatar joomla-cms-bot joomla-cms-bot - change - 12 Oct 2023
Labels Added: No Code Attached Yet
avatar joomla-cms-bot joomla-cms-bot - labeled - 12 Oct 2023
avatar vicn1222 vicn1222 - change - 12 Oct 2023
The description was changed
avatar vicn1222 vicn1222 - edited - 12 Oct 2023
avatar vicn1222 vicn1222 - change - 12 Oct 2023
The description was changed
avatar vicn1222 vicn1222 - edited - 12 Oct 2023
avatar vicn1222 vicn1222 - change - 12 Oct 2023
The description was changed
avatar vicn1222 vicn1222 - edited - 12 Oct 2023
avatar vicn1222
vicn1222 - comment - 13 Oct 2023

I have also tested urls with multiple query string cases, such as using below re-write rule

RewriteRule ^foo-menu/(.*)/(.*)/(.*).html$ index.php/foo-menu?q=$1&dir=$2&name=$3 [NC,L]

Now if I have a browser url

http://localhost/foo-menu/xyz/abc/ijk.html,

$uri = JUri::getInstance() will return

http://localhost/foo-menu?q=xyz&dir=abc&name=ijk

Which is exactly the rewrite rule wants. The existing Joomla routers will now correctly interpret all the path and query strings. Things work beautifully without adding any custom router or parser. By utilizing exisiting Apache features, Joomla becomes much more flexible in turn of SEF urls.

Thank you all for the great work! This is a beatiful framework for any kind of web development!


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42123.
avatar vicn1222
vicn1222 - comment - 13 Oct 2023

One more comment, the Apache rewrite rule is in fact the custom parser / router. We just need to activate it BEFORE entering into the Joomla routers.


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

avatar Quy
Quy - comment - 13 Oct 2023

Please close issue if this has been resolved. Thanks.

avatar vicn1222
vicn1222 - comment - 13 Oct 2023

The changes should be taken by Joomla for next release if it is acceptable. Otherwise a new update will wipe out the temporary change...

Code may need to be cleaned up in the following "else" logic...


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

avatar Quy Quy - change - 13 Oct 2023
Labels Added: Feature
avatar Quy Quy - labeled - 13 Oct 2023
avatar simbus82
simbus82 - comment - 16 Oct 2023

The changes should be taken by Joomla for next release if it is acceptable. Otherwise a new update will wipe out the temporary change...

Code may need to be cleaned up in the following "else" logic...

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

Try to do a Pull Request ;-)

avatar vicn1222
vicn1222 - comment - 16 Oct 2023

How to do pull request? There is no such button or link? Or could someone please do it? Thanks


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

avatar Quy Quy - change - 16 Oct 2023
Labels Added: bug
Removed: Feature
avatar Quy Quy - unlabeled - 16 Oct 2023
avatar Quy Quy - labeled - 16 Oct 2023
avatar simbus82
simbus82 - comment - 16 Oct 2023

How to do pull request? There is no such button or link? Or could someone please do it? Thanks

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

Come to github ;-)
#42123 (comment)
https://github.com/joomla/joomla-cms/pulls

avatar alikon
alikon - comment - 16 Oct 2023
avatar vicn1222
vicn1222 - comment - 16 Oct 2023

After writing all the pull request. I submitted the request and got

"Pull request creation failed. Validation failed: must be a collaborator".

All my writing got lost :-(

Would someone who is a collaborator please do it?

Many thanks


This comment was created with the J!Tracker Application at issues.joomla.org/tracker/joomla-cms/42123.
avatar HLeithner
HLeithner - comment - 17 Oct 2023

After writing all the pull request. I submitted the request and got

"Pull request creation failed. Validation failed: must be a collaborator".

All my writing got lost :-(

Would someone who is a collaborator please do it?

Many thanks

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

You have to contact github support, that Message is usually a error produced by the github spam protection

avatar alikon alikon - close - 17 Oct 2023
avatar alikon alikon - change - 17 Oct 2023
Status New Closed
Closed_Date 0000-00-00 00:00:00 2023-10-17 15:52:08
Closed_By alikon
avatar alikon
alikon - comment - 17 Oct 2023

please test #42150

avatar vicn1222
vicn1222 - comment - 17 Oct 2023

please test #42150

Tests have been done in J3 & J4 multiple times. I am just unsure if I made the right pull request. This is the first time I am using github. So please correct me if there is any error in the pull request. I have been using svn all the time.

Thank you for the wonderful project!

-Frank

avatar alikon
alikon - comment - 17 Oct 2023

@vicn1222 thank yuo for your 1st pr
?

Add a Comment

Login with GitHub to post a comment