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.
RewriteRule ^foo-menu/(.*).html$ index.php/foo-menu?q=$1 [NC,L]
The browser url http://localhost/foo-menu/xyz.html thus becomes http://localhost/foo-menu?q=xyz according to the rewrite rule.
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.
Expect to obey .htaccess rewrite rule when generating the url.
.htaccess rewrite rule is ignored.
Joomla! 4.3.4
Red Hat Apache server
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];
}
Labels |
Removed:
?
|
Labels |
Added:
No Code Attached Yet
|
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.
Please close issue if this has been resolved. Thanks.
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...
Labels |
Added:
Feature
|
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 ;-)
How to do pull request? There is no such button or link? Or could someone please do it? Thanks
Labels |
Added:
bug
Removed: Feature |
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
this reading may help https://docs.joomla.org/Using_the_Github_UI_to_Make_Pull_Requests
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
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
Status | New | ⇒ | Closed |
Closed_Date | 0000-00-00 00:00:00 | ⇒ | 2023-10-17 15:52:08 |
Closed_By | ⇒ | alikon |
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
I have also tested urls with multiple query string cases, such as using below re-write rule
Now if I have a browser url
$uri = JUri::getInstance() will return
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.