Forum Moderators: phranque

Message Too Old, No Replies

How to write a mod rewrite a subfolder to its own child subfolder

         

marktaff

7:34 am on May 27, 2010 (gmt 0)

10+ Year Member



Hi all. I've been struggling for several hours with this; I hope someone can help.

I have websvn installed at http://example.org/websvn/2.3.1/
I want to create a rewrite rule(s) to send any requests for items in the 'websvn' level to websvn/2.3.1,

e.g.:
http://example.org/websvn, or
http://example.org/websvn/index.html, or
http://example.org/websvn/any_other_file.htm

should all go to http://example.org/websvn/2.3.1/

The site is running Joomla, so there are some preexisting rewrite rules.

Below please find the current http://example.org/.htaccess:

Thanks,

Mark

++++++++ BEGIN ++++++++++++++++
## 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 set a mosConfig value through the URL
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


# TAFF: Send requsts for websvn root folder to current version:
RewriteRule ^(.*)/websvn$ $1/websvn/2.3.1 [L,R=301]

# 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
#
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
++++++++ END ++++++++++++++++

[edited by: jdMorgan at 12:28 pm (utc) on May 27, 2010]
[edit reason] example.org [/edit]

jdMorgan

12:32 pm on May 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the specific problem you're encountering?

It looks like only requests for *exactly* "/<something>/websvn" will get rewritten to "/<something>/websvn/2.3.1" -- That is, no sub-pages or other resources in the "/<something>/websvn/2.3.1" directory will be accessed.

But we need to know what you wished to accomplish, what your test results were, and how those results differed from your expectations/desires/requirements.

That Joomla code can be sped up significantly as well... :)

Jim

marktaff

4:48 pm on May 27, 2010 (gmt 0)

10+ Year Member



Thanks Jim.

The problem is that there is no apparent effect of the rewrite rule. Requests for http://example.org/websvn are not being redirected to http://example.org/websvn/2.3.1. Instead, apache is showing a folder index.

Websvn itself wasn't ever installed at example.org/websvn; it was installed to example.org/websvn/2.3.1, and future versions will be installed to example.org/websvn/2.4.0, for example.

So I'm not trying to capture some request string from an old install location of websvn and move it some new install location. Rather, I want to be able to use the url [[]example.org...] and have it refer to a specific installed version of websvn, where I will edit the rule to define which version to point to when I install a new version of websvn.

Hope that clarifies things, and thanks.
--Mark

g1smd

9:58 pm on May 27, 2010 (gmt 0)

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



One important point here is whether you want a redirect or a rewrite.

What exactly do you mean by "should all go to" here?

Your first post contains code for a rewrite, but you start talking about redirects in the second post.

marktaff

10:49 pm on May 27, 2010 (gmt 0)

10+ Year Member



nm. I'll figure it out myself.

jdMorgan

12:04 am on May 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



We're asking questions to clarify the goal, because the code is inconsistent with the stated requirements -- so that you don't end up getting the the right answer to the wrong problem.

Your rule:

RewriteRule ^(.*)/websvn$ $1/websvn/2.3.1 [L,R=301]

Rewrites only the single URL-prototype "example.com/<something>/websvn to /<something>/websvn/2.3.1

Requests for "example.com/<something>/websvn<anything here> will not be matched; therefore, /websvn/2.3.1/ becomes a "single-page directory" -- nothing else in it will be accessible.

Your rule also contains a security risk, in that the client controls the root path.

Further, it will loop forever as-written.

From what I have read here, I'd suggest:

# Exclude numerical subdirectories of /websvn from being rewritten again (to avoid recursion)
RewriteCond $2 !^/[0-9](\.[0-9]+)+$
# Internally rewrite /xyz/websvn<anything> requests to /xyz/websvn/2.3.1<anything>
RewriteRule ^([^/]+)/websvn(/.*)?$ /$1/websvn/2.3.1$2 [L]

Further, you can significantly improve the performance of your server and the lifespan of your hard drive by re-arranging/fixing the Joomla code:

# Copy HTTP Authorization request header to HTTP_AUTHORIZATION variable
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} (\.(php|html?|feed|pdf|raw)|/[^.]*)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

Jim