Forum Moderators: phranque

Message Too Old, No Replies

Rewrite to remove directory from URL

         

dlwilson007

3:10 pm on Sep 10, 2010 (gmt 0)

10+ Year Member



I am attempting to remove the directory where my web site is located from the URL. But I also need to validate my .htaccess setup.

I have an .htaccess file at the root with the following content (removed "http:" so it would display the URL):



Redirect permanent /index.html www.riococokids.com/joomla/



The site is under the joomla directory, but I want the URL to read as follows: www.riococokids.com/

I also have an .htaccess file in the joomla directory with the following content:



##
# @version $Id: htaccess.txt 14401 2010-01-26 14:10:00Z louis $
# @package Joomla
# @copyright Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
# @license [gnu.org...] GNU/GPL
# Joomla! is Free Software
##


#####################################################
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE
#
# 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!
#
## Deny access to extension xml files (uncomment out to activate)
#<Files ~ "\.xml$">
#Order allow,deny
#Deny from all
#Satisfy all
#</Files>
## End of deny access to extension xml files
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%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})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]
#
########## End - Rewrite rules to block out some common exploits

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

RewriteBase /joomla

########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section



The above .htaccess comes directly from the Joomla installation and provides the basic security and SEF needed to run the site.

So I have a couple of questions.

  1. Is it valid to be running two .htaccess files, one at the root doing the redirect, and the second in the Joomla folder providing the security and SEF?
  2. How do I write the rewrite rule to remove "joomla" from the url?
  3. Where would I put the rule as I seem to remember in the past that order matters?

I know bits and pieces of my problem have answers out in the forums, but I can't seem to gather enough info to make a complete answer, so I have been spinning in circles trying to solve this problem, and it seems like it should have a fairly simple solution.

Thanks for any help!

dlwilson007

8:52 pm on Sep 15, 2010 (gmt 0)

10+ Year Member



I make it hard on myself because I start considering symlinks and app folders. Maybe this will make it easier - desired URL to path mapping.

1. riococokids.com -> /home/riocock/public_html/joomla/
2. www.riococokids.com -> /home/riococok/public_html/joomla/
3. riochilddev.riococokids.com -> /home/riococok/railsapps/riochilddev/current/public/

My riochilddev subdomain points to a symlink in public_html, called riochilddev. That symlink points to the third folder above.

Would it make any sense to move joomla out and create a symlink, or does that even matter?

And how do I instruct apache to skip all rules processing to the subdomain but still process the passenger settings? Does it look at the subdomain, the symlink, or the path, which right now shows www.riococokids.com/riochilddev

By the way...since my htaccess is not written correctly, some links show errors, in case anyone follows them.

I hope I'm explaining things clearly...learning a ton, but I apologize where I miss the mark.

jdMorgan

3:08 pm on Sep 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use
 RewriteCond %{HTTP_HOST} ^riochilddev\.riococokids\.com 

and
 RewriteCond %{HTTP_HOST} ^(www\.)?riococokids\.com 

to distinguish which rules should be applied and which should not.

Also, don't get confused about URLs, filepaths, and symlinks.

The RewriteRule directive looks only at the requested URL-path. Depending on the syntax that you specify, it either rewrites the request to an internal filepath, or it generates a redirect response to the client, telling it to re-request the desired object using the specified new URL.

Symlinks are not explicitly 'recognized' by the RewriteRule directive when doing an internal rewrite. The RewriteRule outputs a filepath, and if that filepath is a symlink, then it is handled by the server's operating system filehandler, and that handling is of no concern to mod_rewrite itself.

The RewriteCond directive can check any and all server variables, and so can be used to look at hostnames and many other non-URL-related factors. I'd suggest that you look into the RewriteCond directive description in the mod_rewrite documentation at Apache.org. Also note the "Rule processing" section of this documentation, because it has important information in it that can greatly affect the efficiency of the code that you write.

Jim

dlwilson007

1:25 am on Sep 29, 2010 (gmt 0)

10+ Year Member



Ok, after all this, I both have a solution and feel like an idiot. But, we have to learn somehow!

So the solution is to treat Joomla and my rails application as separate apps. I was using my subdomain to point to a symlink inside the public_html, which was then pointing to my app file path. However, all I needed to do was to point the subdomain directly to the file path and treating it as a completely separate app that never sees the htaccess file.

So from here on out, I'll just create new apps or joomla instances outside of the public_html, and then they can each have their own htaccess.

Wow...that was a lot of work for such a simple solution. Isn't that often how it works?!?

g1smd

11:30 am on Sep 29, 2010 (gmt 0)

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



Yep. But at least the Joomla folks now have a solution for speeding up their mod_rewrite code by a factor of 2 or 3... if they ever get round to reviewing it.

g1smd

9:24 pm on Nov 7, 2010 (gmt 0)

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



The Joomla patch has still not been reviewed. It's been almost two months now.

g1smd

12:57 pm on Feb 20, 2011 (gmt 0)

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



Finally, the new Joomla .htaccess patch is in pending state.

The latest version of the proposed new code is:

##
# @version$Id$
# @packageJoomla
# @copyrightCopyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
# @licenseGNU General Public License version 2 or later; see LICENSE.txt
##

##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# 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).*script.*(\>|%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})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.

##
# 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}]
#
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

g1smd

5:09 pm on Mar 1, 2011 (gmt 0)

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



Updated code for Joomla 1.5 and Joomla 1.6 can be found here: [webmasterworld.com...]

g1smd

8:59 pm on Apr 4, 2011 (gmt 0)

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



The updated code is now committed in
Joomla 1.5.23
and
Joomla 1.6.2
both of which will (probably) be released late this week or early next week.

g1smd

9:06 pm on Apr 4, 2011 (gmt 0)

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



Please make the following changes to the post three up the page from this one (and then delete this post):

Replace
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]

with
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]


Replace
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]

with
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]


Replace
# Send all blocked request to homepage with 403 Forbidden error!

with
# Return 403 Forbidden header and show the content of the root homepage

g1smd

9:26 pm on Apr 4, 2011 (gmt 0)

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



The URL in post #msg4201889 should be

http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=22425



Please fix it and remove this post. :)
This 40 message thread spans 2 pages: 40