How to redirect to a subdirectory with .htaccess and have the subdirctory part hidden

I have a site at [noparse]http://www.mradlmaier/[/noparse]

The site resides in the subdirectory /joomla25-development/ (It is a Joomla site).

Now I am want a couple of things:

  1. have [noparse]http://www.mradlmaier/[/noparse] redirect to [noparse]http://mradlmaier/[/noparse] so that search engines dont see it as duplicate content.
  2. redirect access to the root to the subdirectory [noparse]http://www.mradlmaier/[/noparse]
  3. but have the subdirectory name hidden in the address bar of the browser

So the first 2 things work:
So when I request [noparse]http://www.mradlmaier/[/noparse], I will land correctly [noparse]http://www.mradlmaier/[/noparse] and the browser displays [noparse]http://mradlmaier/[/noparse]

However, if I click any link, the redirect still works, but the subdirectory name shows up in the browser:


instead of:


Here is my .htaccess:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mradlmaier.bplaced\\.net$ [NC]
RewriteRule ^(.*)$$1 [R=301,L]

# .htaccess primary domain to subfolder redirect 
# Copy and paste the following code into the .htaccess file 
# in the public_html folder of your hosting account 
# make the changes to the file according to the instructions.
# Do not change this line.
RewriteEngine on
# Change to be your primary domain.
RewriteCond %{HTTP_HOST} ^(www.)?$
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteCond %{REQUEST_URI} !^/joomla25-development/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteRule ^(.*)$ /joomla25-development/$1
# Change to be your primary domain again. 
# Change 'subfolder' to be the folder you will use for your primary domain 
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?$ 
RewriteRule ^(/)?$ joomla25-development/index.php [L]

Can anyone please advice?

Yes, If you mess with the “canned .htaccess” of a “canned application,” you will only cause problems (with the exception of the WordPress code which has some major problems - IMHO, of course). Since you’ve “messed” with it already, it’s likely you’ll have to restore the original .htaccess Joomla built for you then come back asking for changes to suit specific needs.



Joomla does not use a .htaccess per default. Joomla, out-of-box, doesnt use .htaccess.
However, a template htaccess.txt comes with the Joomla installation. Here it is:

# @package		Joomla
# @copyright	Copyright (C) 2005 - 2012 Open Source Matters. All rights reserved.
# @license		GNU General Public License version 2 or later; see LICENSE.txt

# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations.  It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that dissallows changing it in
# your .htaccess file.  If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's.  If they work,
# it has been set by your server administrator and you do not need it set here.

## Can be commented out if causes errors, see notes above.
Options +FollowSymLinks

## Mod_rewrite in use.

RewriteEngine On

## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\\([^)]*\\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\\[|\\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\\[|\\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
## End - Rewrite rules to block out some common exploits.

## Begin - Custom redirects
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
## End - Custom redirects

# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).

# RewriteBase /

## Begin - Joomla! core SEF Section.
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\\.php
# and the request is for something within the component folder,
# or for the site root, or for an extensionless URL, or the
# requested URL ends with one of the listed extensions
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\\.(php|html?|feed|pdf|vcf|raw))$ [NC]
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
## End - Joomla! core SEF Section.

I think, Joomla assumes that it is installed in the root directory per default, which I dont want to do for a variety of reasons.

Thanks for that listing! I don’t use Joomla so that’s the first I’ve seen of their code.


Options FollowSymLinks should be in the httpd.conf already.

The first block should loop on index.php without an escape RewriteCond (you can avoid that problem by adding an OR flag to the last RewriteCond then adding either RewriteCond %{REQUEST_URI} !-f or RewriteCond %{REQUEST_URI} !^index\.php$.

The RewriteCond %{REQUEST_URI} !^/index\.php statement is not only a duplication of the RewriteCond %{REQUEST_URI} !-f but it’s also only valid on an Apache 1.x server (because of the leading /).

If you want to use mod_rewrite, I’d move it to the directory which contains your Joomla code so you’re not concerned about the RewriteBase nor interfering with other, valid file requests.



So, your advise would be using 1 .htaccess in the root directory to take care the redirect to the subdirectory and have a second .htaccess in the subdirectory which takes care the url rewriting?
Sounds more managable and cleaner to me…


Well, sort of! The DocumentRoot’s .htaccess would handle SITE-WIDE tasks while the Joomla directory’s would handle Joomla tasks. The {REQUEST_URI} should be used to send the visitor through the site-wide tasks to the Joomla tasking then on to Joomla’s output.

I agree, separating the two make things more managable because it’s simpler to understand the logic.