Search code examples
apache.htaccesshttp-redirectjoomla

Skip Joomla from the URL


My site runs on Joomla, which is installed in http://example.com/joomla, but my URL is still http://example.com (I access the Joomla folder from the root). I find it good to have joomla separated from other files and folders on my site, but irrelevant that visitors see joomla in the URL, since it's better they see only things in the URL that has more to do with what they are searching for on my site. So I thought let's skip joomla from the URL. And because of Cloudflare all URLs must be www-URLs in order to work properly.

I was able to achieve this thanks to the below solution:

https://stackoverflow.com/a/35047905/5681573

My modified version:

.htaccess in the root:

RewriteEngine on
RewriteCond %{THE_REQUEST} !joomla/
RewriteRule ^(.*)$ /joomla/$1 [L]

.htaccess in the joomla/ subdirectory:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

In my solution is included redirection from non-www URLS to www-URLs and from http to https.

In joomla/configuration.php I set $live_site = "https://example.com" and changed the tmp and log-folders to:

  • myserverpath/public_html/joomla/administrator/logs
  • myserverpath/public_html/joomla/tmp

Everything looks fine. Now when I test it these are the results:

But there is a problem with non-www URLs:

Joomla is not skipped from the URL when non-www URLs are redirected to www-URLs. All other links that begin with www are correctly showing without 'joomla' in the URL. No matter if you type the address directly or if you arrive there via a link or button on the website: joomla is further always skipped from any www-URL. It's just that moment that a non-www URL is redirected to a www-URL that it's not working and the word 'joomla' is appearing in the URL.

And also 1 exception is the admin URL, which also is a problem, unique because it's failing to skip Joomla from the URL both when used the www-URL as well as the non-www-URL:

I searched hours and hours without result.

Some things I tried to solve this:

  • changing $live_site to '' (not helping)
  • changing $live_site to 'https://www.example.com' (not helping)
  • changing $live_site to 'https://example.com' (not helping)
  • changing RewriteBase in joomla/.htaccess to RewriteBase / (no change at all)
  • changing RewriteBase in joomla/.htaccess to RewriteBase /joomla/ (no change at all)

Some useful hints or possible solutions are very welcome. Thanks in advance.

Edit because of the solution answer:

About the first three lines of codes in the solution answer that I posted on this page: I created them only for the admin URL:

    RewriteCond %{REQUEST_URI} (.*)administrator$
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]

Although joomla is skipped from all URLs successfully (see answer with solution), for me the question remains why it's needed to add the trailing slash as you see above, in order to solve it for the admin URL. Otherwise, when you don't add these three lines of codes the admin URL will redirect to joomla/administrator and will show joomla in the URL. Does this have anything to do with the defines settings?... Who knows...


Solution

  • I'm glad to say the solution for my issue is found.

    For anyone who has the same problem, here is the solution:

    • no need to have any rewriterule in the htaccess of the joomla/ directory, just don't write rewriterules in that htaccess file
    • in the root htaccess file take care of where you add the rewriterules: you should put them on TOP of the htaccess file. These codes will do the trick:

      # keep codes on top !!!
      RewriteEngine On
      RewriteCond %{REQUEST_URI} (.*)administrator$
      RewriteCond %{REQUEST_URI} !/$
      RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
      RewriteCond %{HTTP_HOST} !^www\. [NC]
      RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
      RewriteCond %{THE_REQUEST} joomla/
      RewriteRule ^joomla/(.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
      RewriteCond %{REQUEST_URI} !joomla/
      RewriteRule ^(.*)$ /joomla/$1 [L]
      

    Thanks and credits to the great SiteGround support team (my provider), as they were able to find the solution to make everything work as expected.