A J4 SEF related problem in .htaccess

Be informed that this forum is not an official support forum for Joomla! 4.0. Any issues regarding Joomla! 4.0 must be reported at https://issues.joomla.org/.

Joomla! 4.0 is still in its Alpha stage. This forum should be used for sharing information about Joomla! 4.0.

Moderator: ooffick

Post Reply
ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

A J4 SEF related problem in .htaccess

Post by ceford » Wed Dec 04, 2019 8:40 am

To avoid copying images from my production site to my test and development sites I have been in the habit of using this img construct:

<img src="[local url]" onerror="this.onerror=null;this.src='[production url]" ... />

I just noticed that every missing local resource file causes a load of the J4 application stack to render the Page not Found template, which is never seen. I am not sure I like this behaviour! I have got around it for now by adding a rewrite condition from the J3 .htaccess file. And I suppose I should rewrite the code to test for the local file and not rely on the error method. I mention it here because it is a bit of a pain during debugging and not at all obvious for anyone migrating from J3.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2433
Joined: Sun May 04, 2008 12:37 pm

Re: A J4 SEF related problem in .htaccess

Post by waarnemer » Wed Dec 04, 2019 9:10 am

to see the error, there must be an error... the error is the page/file not found.. that is your trigger needed to go for the production image...

so you could:
mount the same image folder in all your instances.. if you have control over all your servers you can.. though when making changes in dev you will then also see the changes to the images in production.
you could also sync using a cron job for instance... making sure it only is one way...

on a smalll digital signage project I did years ago I synced content using nextcloud instances on raspberry pies... that was non joomla though.... but the idea would be similar...

there is tons of ways doing so...


Another thing... this has nothing to do with SEF (search engine friendly)....

ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

Re: A J4 SEF related problem in .htaccess

Post by ceford » Wed Dec 04, 2019 9:55 am

The SEF section of htaccess changed between J3 an J4:

## Begin - Joomla! core SEF Section.

In particular this line disappeared in J4:

RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]

so the request for a non-existent resource is redirected to index.php

Clearly there are lots of solutions for those aware of the problem.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2433
Joined: Sun May 04, 2008 12:37 pm

Re: A J4 SEF related problem in .htaccess

Post by waarnemer » Wed Dec 04, 2019 10:18 am

Your are trying to feed your dev site with production resources
That is not so much of a default situation. Rewrites in the .htaccess do not have much to do with that.
Also there is no mention of your .jpg, .png, .gif etc in that condition..

By the way.. your condition is not in the default provided htaccess file in j! 3.9 also... it did not disappear... if it was in your j!3.9 someone put it there...

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 37547
Joined: Sat Apr 05, 2008 9:58 pm

Re: A J4 SEF related problem in .htaccess

Post by Webdongle » Wed Dec 04, 2019 10:39 am

Your local environment does not serve files to the internet. So when your browser pulls the files from the site then the images on your computer will not be available to be displayed. The images must be on a Host computer (one that serves files to the internet) in order to be reached by the site.
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"The definition of insanity is doing the same thing over and over again, but expecting different results": Albert Einstein

ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

Re: A J4 SEF related problem in .htaccess

Post by ceford » Wed Dec 04, 2019 1:00 pm

To simplify what I am saying - I have a fully functional on-line (but private) J4 Test Site with J4 Alpha 11 installed. If I type in a url like this:

https://[mydomain]/images/garbage.jpg

I get a Joomla generated Error page complete with side bar main menu and login form. I think I would prefer to get the standard Apache 404 not found response. Is this a misguided preference?

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 37547
Joined: Sat Apr 05, 2008 9:58 pm

Re: A J4 SEF related problem in .htaccess

Post by Webdongle » Wed Dec 04, 2019 1:49 pm

Will that url work directly in the browser?
N.B. it is not good practice to hard code url's to images. Much better to have them on the same server and use relative paths.
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"The definition of insanity is doing the same thing over and over again, but expecting different results": Albert Einstein

mbabker
Joomla! Hero
Joomla! Hero
Posts: 2236
Joined: Sun Feb 28, 2010 8:26 pm

Re: A J4 SEF related problem in .htaccess

Post by mbabker » Thu Dec 05, 2019 12:19 am

You may have some extra rewrite rules to have Apache handle 404s on your production server instead of having Joomla handle them, which in a lot of cases would be a good idea (having the web server serve 404 responses when you can is going to be better on resources than booting a PHP application and going through it to create a 404 response).

The line you specifically posted doesn't exist in the htaccess.txt file that Joomla ships, either in 3.x or 4.0, and that line wouldn't do much to deal with items in the images folder anyway.

ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

Re: A J4 SEF related problem in .htaccess

Post by ceford » Thu Dec 05, 2019 3:03 am

mbabker: thank you for your first comment - it is reassuring to know I am not suggesting something silly.

On the second point the htaccess line in question is in this post from 2015: https://joomla.stackexchange.com/questi ... rect-issue so must have been in the Joomla distribution at some time. I can see that it is doing what I need by only redirecting urls with page-like extensions (.php, .html, etc).

Anyway, I think this issue was worth flagging for those of us tolerating broken image links.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2433
Joined: Sun May 04, 2008 12:37 pm

Re: A J4 SEF related problem in .htaccess

Post by waarnemer » Thu Dec 05, 2019 7:16 am

true you are correct.. investigation showed it was in 3.3.0 to be taken out in 3.3.1

https://developer.joomla.org/joomlacode ... 29425.html
see bottom solution:
https://github.com/joomla/joomla-cms/co ... cbfb8759fd

so by default, if you did not install and enable other redirect/rewrite plugins, tools, anything, you will get a 404 on any that does not exist in your site.





Sidenote... I do think people are not really aware... On updating/upgrading a Joomla! site it is good habit to check the release notes and check for changes made in the htaccess... as that one is not automatically updated/upgraded...

ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

Re: A J4 SEF related problem in .htaccess

Post by ceford » Fri Dec 06, 2019 7:11 am

I think there is a case for putting a rewrite condition in the distribution htaccess file that avoids Joomla rendering a Page not Found page for missing resources such as images and js and css files on the grounds that in those cases the PnF page is not displayed by the browser. Much better to let Apache send its simple response. Even if the condition is commented out it would be helpful for it to be there for the benefit of anyone wanting to use it.

waarnemer
Joomla! Hero
Joomla! Hero
Posts: 2433
Joined: Sun May 04, 2008 12:37 pm

Re: A J4 SEF related problem in .htaccess

Post by waarnemer » Fri Dec 06, 2019 4:03 pm

why would you want a missing image file to redirect?
when <img> is not found it is not found... and in order to redirect to something else on the event an image isn't there... first you need to determine it isn't there... that means notice the error.

If you do that in htaccess fine.. in your case of development -> production it may even be a desired rule. But in general it isn't "best practice" for the usual site that whishes to be indexed by the search engines with the correct content.

ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

Re: A J4 SEF related problem in .htaccess

Post by ceford » Sat Dec 07, 2019 2:10 am

"why would you want a missing image file to redirect?"

That is the whole point. I don't! Try it on your own site (or the Joomla site) with a url like this:

[your domain]/images/garbage.jpg

With the current htaccess file Joomla loads up its application framework and sends a complete 'Page not Found' page. Fine if you are typing in garbage. But if the link is an image embedded in a web page the browser discards the response page. So much better to get a 404 response directly from Apache. And that needs an extra condition in htacccess. That is what I am suggesting.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 37547
Joined: Sat Apr 05, 2008 9:58 pm

Re: A J4 SEF related problem in .htaccess

Post by Webdongle » Sat Dec 07, 2019 10:01 am

ceford wrote:
Sat Dec 07, 2019 2:10 am
"why would you want a missing image file to redirect?"

That is the whole point. I don't! ...
What url is being rendered that fetches the 404?
What url is the actual url of the image and have you tried that url directly in the browser?
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"The definition of insanity is doing the same thing over and over again, but expecting different results": Albert Einstein

ceford
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Mon Feb 24, 2014 10:38 pm

Re: A J4 SEF related problem in .htaccess

Post by ceford » Sat Dec 07, 2019 11:33 am

I found this htaccess rewrite condition in a stack overflow post:

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]

It does exactly what I suggest. I am reluctant to type in a deliberately broken url here for others to test. So I repeat, try it on your own domain or the jooml site - [your domain]/images/garbage.jpg - without this rule you will get back a Joomla generated Error Page. And on reflection I should add .svg to the rule.

https://stackoverflow.com/questions/342 ... -condition

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 37547
Joined: Sat Apr 05, 2008 9:58 pm

Re: A J4 SEF related problem in .htaccess

Post by Webdongle » Sat Dec 07, 2019 1:31 pm

If you have a url that will not work then perhaps it is better to find out why the browser can't feth the url.
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"The definition of insanity is doing the same thing over and over again, but expecting different results": Albert Einstein


Post Reply

Return to “Joomla! 4 Related”