Forum Moderators: phranque

Message Too Old, No Replies

htaccess trying to hide joomla subfolder in address field

         

Trudd

6:38 pm on Apr 25, 2012 (gmt 0)

10+ Year Member



I have a joomla website set up with a couple of Rewrites in htaccess that I cannot figure out how to fix.

The content below is some of the content in the htaccess files, and other than the last two lines it all works more or less as expected. (if it's optimal is another matter)

In short I try to all make sure that addresses are reformatted to www.example.com not matter what it typed from the beginning
This is true except for a few subfolders excluded from the rewrites.

So for example dev.example.com example.com www.example.com/pre are all reformatted,
but not www.example.com/drupal and www.example.com/test . Neither is www.example.com/joomla and perhaps this is part of the problem?

What I'm aiming for is for non-excluded subfolders e.g. www.example.com/pre to be rewritten as www.example.com and not www.example.com/joomla.
The problem is that I don't know what I can place as substitute in RewriteRule to replace the URI /pre with an empty string.

But most importantly I have joomla installed in a subfolder in the root. And I was hoping there was a way to do a rewrite so all paths directed to the joomla folder will have the folder name hidden in the address field.
The index.php part is already hidded using joomla settings, so only the joomla folder needs to be removed.
In other words I would like www.example.com/joomla to be shown as www.example.com (partly works) and www.example.com/joomla/blog shown as www.example.com/blog.

I have tried many suggestions around the web but no matter what I try the joomla folder keeps showing.
Is there anyone here with suggestions?

RewriteCond %{REQUEST_URI} !(\.|/$)
RewriteRule (.*) http://www\.example\.com/$1/ [R=301,L]

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?(.*)$ http://www\.example\.com/$1 [r=301,L]

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www\.example\.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/(drupal|wordpress|joomla|dev|test|workingbackup) [NC]
RewriteRule ^.*$ - [L,S=2]

RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{REQUEST_URI} ^/(.+) [NC]
RewriteRule ^(.*)$ ?/ [L]

RewriteRule ^(.*)$ joomla/$1 [L]

(also tried the version using {REQUEST_FILENAME} !-f och {REQUEST_FILENAME} !-d but without any success)

[edited by: incrediBILL at 8:30 pm (utc) on Apr 25, 2012]
[edit reason] fixed links and exemplified domain [/edit]

lucy24

10:00 pm on Apr 25, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?(.*)$ http://www.example.com/$1 [r=301,L]

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


You want a single Rewrite at the end of all other Rewrites, where it will only pick up those requests that have not already been redirected. (That is the prescriptive "you want", not descriptive. You want it because we say you do.) It should say

:: fingers crossed, as usual ::

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1

That means (or, ahem, is intended to mean) "If the requested domain is not exactly www.example.com or exactly nothing (something about HTTP/1.0, I think) then change it".

Oh, and Preview is your friend, because it forced me to notice that I'd retained the escaped periods in the target. Some RegEx dialects don't care if you escape things that don't require escaping, but some will introduce literal backslashes. Apache is very unforgiving.

(also tried the version using {REQUEST_FILENAME} !-f och {REQUEST_FILENAME} !-d but without any success)

I hope you didn't say that to .htaccess, because it does not speak Swedish ;) The -f and -d conditions should be used only as a last resort, because it places a whole new load on the server. Basically it has to look for each file or directory twice, first from within mod_rewrite and then again later when it actually goes there.

Trudd

10:10 pm on Apr 25, 2012 (gmt 0)

10+ Year Member



I had a further look on my code and managed to clean it up a bit. Hopefully this will help the readability.

I had a further look on my code and managed to clean it up a bit. Hopefully this will help the readability.

Rule 1 & 2 removed

#Rule 3
#If host isn't www.example.com rewrite to www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ h**p://www\.example\.com/$1 [R=301,L]

#Rule 4
# If URI begins with any of these folder names do nothing
RewriteCond %{REQUEST_URI} ^/(drupal|wordpress|dev|test|workingbackup) [NC]
RewriteRule ^.*$ - [L]

#Rule 5
#If there is a subfolder typed right after www.example.com that isn't part of the exceptions earlier
# e.g. www.example.com/pre the rewrite to www.example.com
#Without this rule the text remains after the domain name, i.e. www.example.com/pre
#I want to replace this text with {empty_string} . This is just cosmetics I guess.
RewriteCond %{REQUEST_URI} ^/(.+) [NC]
RewriteRule ^(.*)$ {empty_string} [L]

#Rule 6
#Rewrite URI to joomla/$1
#What I'm trying to accomplish is to be able to type
#www.example.com/blog and not have to type www.example.com/joomla/blog
#to arrive at the right location. In other words I do not want the joomla folder to show in the address field.
#So really I'm trying to remove the joomla folder rather than adding it...
RewriteRule ^(.*)$ joomla/$1 [L]

#Rule 6-Suggesionn
# This was an alternative suggestion but I'm afraid it only generates 500 Internal Error for me
#If URI doesn't begin with joomla the rewrite to /joomla/$1
#Include query string
#RewriteCond %{REQUEST_URI} !^/joomla) [NC]
#RewriteRule ^(.*)$ /joomla/$1 [L, QSA]

Trudd

10:24 pm on Apr 25, 2012 (gmt 0)

10+ Year Member



Hi Lucy.

Thanks for suggestion.

Could I ask what the small differences in our two line do? In your example you have ?$ at the end. Not sure what this does. I guess the () is following some syntax standard? I'm not actually working on any Apache interface. I just have a .htacess file in the web-root provided by my web host.

Your line: RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
My Line: RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]

g1smd

11:44 pm on Apr 25, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



#Rule 3 - Read Lucy's post again and use the code given there. Your code redirects only non-www requests. Lucy's redirects all non-canonical requests (even those with port numbers, etc) and is much more robust.

#Rule 5 - I have no idea what you are trying to achieve here. You have an internal rewrite of some sort?

Trudd

7:04 am on Apr 26, 2012 (gmt 0)

10+ Year Member



How do I know when to use escape characters or not?
I cannot find anything in the RewriteCond or RewriteRule syntax at Apache mentioning this.
Is there any simply guidelines for this?

Rule 5
Well I tried do describe it in my post. If a user types in a folder that is not listed in Rule 4,
ie a non-existing subfolder at the moment, instead of trying to go to this subfolder
I would like to rewrite to www.example.com, ie the root. If I leave this rule out the user that
types www.example.com/pre (a non-existing subfolder) will remain on the current page but in the
address field in will now say www.example.com/pre and I would like to avoid this.
I hope this explains it a better.

And then there is Rule 6.
This is the main problem. How to rewrite so the joomla subfolder
doesn't show up but the pages still show up properly...

lucy24

8:48 am on Apr 26, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If a user types in a folder that is not listed in Rule 4,
ie a non-existing subfolder at the moment, instead of trying to go to this subfolder
I would like to rewrite to www.example.com, ie the root.

A rewrite doesn't change the address bar, so there's no point to the Rule.

But speaking of Rule 4:

RewriteCond %{REQUEST_URI} ^/(drupal|wordpress|dev|test|workingbackup) [NC]
RewriteRule ^.*$ - [L]

Why does it have to be structured that way rather than simply

RewriteRule ^(drupal|wordpress|dev|test|workingbackup) - [L]

?

If Rule 5 is supposed to catch any request that's in the form www.example.com/morestuff and send it back to the plain index page, then you need to make it a redirect:

RewriteCond %{THE_REQUEST} /[a-zA-Z]
RewriteRule . http://www.example.com/ [R=301,L]

That isolated dot means "if the request is for anything at all other than the root". But don't cut and paste; we're going to need a second opinion on this. I'm thinking: the request contains a slash followed by at least one letter*. As a bonus, this will also pick up requests for explicit "index.php". You have to look at THE_REQUEST in case mod_dir has already done its stuff.


* I originally said simply /\S --slash followed by any non-space-- and then realized that this applies to all requests, thanks to the HTTP/1.n element. Ouch. Close call. Now, can we hope that none of your directory names begin with a numeral?

Trudd

11:54 am on Apr 26, 2012 (gmt 0)

10+ Year Member



The reason rule 4 was structured that way is simple. It is my lack of knowledg on the subject :-P

I changed both rule 4 & 5 to your suggestions and it is working fine, see current version below.
Thanks a lot for the help Lucy!

But just out of curiosity is there a way to have an empty string as a substitute?
I.e. if I for some reason in the future want to replace a textstring with an empty string?

#Rule 3
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ h**p://www.incumbo.com/$1 [R=301,L]
#Rule 4
RewriteRule ^(drupal|wordpress|dev|test|workingbackup) - [L]
#Rule 5
RewriteCond %{THE_REQUEST} /[a-zA-Z]
RewriteRule . h**p://www.example.com/ [R=301,L]
#Rule 6
#I'm still trying to get this last rule right
RewriteRule ^(.*)$ joomla/$1 [L]

Trudd

5:15 pm on Apr 26, 2012 (gmt 0)

10+ Year Member



I think the .htaccess file in root is functioning properly now. I guess what remains is instead to figure out how to configure the .htaccess file in the joomla subfolder.

Below is the variables and rules for joomla.

Unfortunately the closest I can come is either that the site is fully functional but with joomla in the address field, or the links are shown correctly without joomla in the status field but when I click on one I end up at www.example.com and also the css formatting (I guess) is lost.

joomla/configuration.php values:
$sef = '1';
$sef_rewrite = '1';
$live_site = 'h**p://www.example.com/joomla/';
or
$live_site = 'h**p://www.example.com/';

Options +FollowSymLinks
RewriteEngine On
RewriteBase /joomla
#or
RewriteBase /

#Rule 1
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteRule ^(.*)$ index.php [L]

g1smd

6:39 pm on Apr 26, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Rule 5 in your list should be moved to be first.

Your Rule 1 noted immediately above should be last in your list.

Specify all of your redirects (things with R flag) before all of your rewrites (things without R flag).

Trudd

7:26 pm on Apr 26, 2012 (gmt 0)

10+ Year Member



I found out that one of my rules was causing much of the problems with the joomla folder keep showing in the address field.

Is there a way to integrate this rule with the others without casing any problems?
I tried your suggestion g1smd, and move rule A4 to the top but it is still causing some problem.
Admittedly new ones (loss of CSS formatting for example). :-D

#Rule A4
#Redirect to www.example.com for root subfolders,
#except for the joomla folder and the folders mentioned in rule A2
#This currently doesn't work alongside the other rules
#RewriteCond %{THE_REQUEST} /[a-zA-Z]
#RewriteRule . h**p://www.example.com/ [R=301,L]

Thanks for the help so far Lucy and g1smd!

Below is the fully functioning configuration for my site. Perhaps this can be of some help to others.

-------------------------------------------------

# A) root/.htaccess #

Options +FollowSymlinks
RewriteEngine on
RewriteBase /

#Rule A1
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$ [NC]
RewriteRule ^(.*)$ h**p://www.example.com/$1 [R=301,L]

#Rule A2
RewriteRule ^(drupal|wordpress|dev|test|workingbackup) - [L]

#Rule A3
RewriteCond %{REQUEST_URI} !^joomla [NC]
RewriteRule ^(.*)$ joomla/$1 [L]

# B) root/joomla/.htaccess #

Options +FollowSymLinks
RewriteEngine On
RewriteBase /joomla

#Rule B1
RewriteCond %{REQUEST_URI} !^index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteRule ^(.*)$ index.php [L]

// root/joomal/configuration.php //

$sef = '1';
$sef_rewrite = '1';
$sef_suffix = '0';
$live_site = 'h**p://www.example.com/';

g1smd

7:38 pm on Apr 26, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Combine rule A3 and B1.

Completely omit the second incarnation of
Options +FollowSymLinks
RewriteEngine On
RewriteBase /joomla

Within the pages of your site, links to files should begin with a leading slash.

Trudd

7:57 pm on Apr 26, 2012 (gmt 0)

10+ Year Member



Thanks I will give it a try.

I celebrated to early though. It all looks well. But there is one slight problem with my ealier solution. I cannot access the joomal/administrator forlder. That means I cannot log in and work on the site. :-P

Trudd

8:32 pm on Apr 26, 2012 (gmt 0)

10+ Year Member



Not managed to successfully merge the two rules you suggested.

But I have found a solution that works for now to be able to access the joomal administrator interface.

I added the below rule to joomla\.htaccess

RewriteRule ^(administrator) - [L]

lucy24

11:10 pm on Apr 26, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



But just out of curiosity is there a way to have an empty string as a substitute?
I.e. if I for some reason in the future want to replace a textstring with an empty string?

Sure, it's trivial in mod_rewrite. Unlike mod_alias, it doesn't reappend any part of the path; it does a full replacement. Some basic patterns:

#1 RewriteRule ^somedirectory http://www.example.com/ [R=301,L]

= if the request is for a particular directory or anything inside it, redirect to the root.

#2 RewriteCond %{THE_REQUEST} somedirectory/[a-z][A-Z]
RewriteRule ^somedirectory/.+ http://www.example.com/somedirectory/ [R=301,L]

or (with a similar Condition)

#3 RewriteRule ^(gooddirectory1|gooddirectory2)/.+ http://www.example.com/$1/ [R=301,L]

= if the request is for anything inside a particular directory, redirect to the requested directory's index file. Here you have to look at THE_REQUEST to make sure you're not sending /index.html into an infinite loop if mod_dir has already done its stuff. (You can omit this if "somedirectory" doesn't really exist but is just used in URLs.)

#4 RewriteRule ^somedirectory/(.*) http://www.example.com/$1 [R=301,L]

= version to use if you have dumped a directory and moved all its pages to the root.

It makes no difference whether any of those Redirect targets actually exist. All you're concerned with is what the user sees in their browser's address bar.