Forum Moderators: phranque

Message Too Old, No Replies

Quick rewrite question

mod_rewrite help

         

aceBoB

3:34 am on Jul 14, 2009 (gmt 0)

10+ Year Member



Hi all,

Just registered, wanted to seek some advice from a community rather then spend additional hours jumping from search result to search result..

I just have what is proberly a very simple rewrite question for the regulars here.

I have a folder in the root of my site.. let's say it's called "test"..

So..

/test/
/another folder/

and so on...

Inside my folder (test) i store files for downloading. Rather then query these files directly i'd like to do something like..

mysite.com/download/txt/filename

Download simply indicates it's a download URL (non-existent folder), txt refers to the file extension, and filename, the name of the file... so again non-existent directories, i just want to use them in the URL writing...

So let's take an example..

mysite.com/test/readme.txt
becomes..
mysite.com/download/txt/readme

I've been copying and pasting various examples for the last 2 hours and i'm still not getting the result i'm after.. :(

I've tried bits like this...

RewriteCond %{QUERY_STRING} ^download/(.*)/(.*)$ [NC]
RewriteRule ^/test$ $2\.$1 [NC]

I'm proberly way off, or misunderstanding the usages..

Appreciate any help or examples...

Regards,
Ace Bob

jdMorgan

5:12 pm on Jul 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only search result you need pay any attention to is the one that points to the mod_rewrite documentation at apache.org... Go to the source.

Your code is over-complicated, and in addition to the incorrect reference to a non-existent query string, has a common flaw: multiple ".*" subpatterns -- which are hugely inefficient.

I'd suggest:


# Internally rewrite request for /download files to /test subdirectory (with re-formatting)
RewriteRule ^download/([^/]+)/[^./]+$ /test/$2.$1 [L]

This rule uses negative-match subpatterns, which allows it to match requested URL-paths in a single left-to-right pass, as opposed to the dozens or hundreds of back-off-and-retry matching attempts that will result from using multiple ".*" subpatterns.

You may also wish to prevent direct HTTP access to the /test subdirectory by adding a second rule (ahead of the one above):


# Forbid direct access to /test subdirectory
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test/[^\ ]*\ HTTP/
RewriteRule ^test/ - [F]

or alternately:

# Redirect direct client requests for /test files back to canonical /download URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /test/[^\ ]*\ HTTP/
RewriteRule ^test/([^./]+)\.([^./]+)$ http://www.example.com/download/$2/$1 [R=301,L]

See the resources cited in our Apache Forum Charter for more info.

Jim

aceBoB

9:47 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Many thanks for help, almost there...

<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect direct client requests for /test files back to canonical /download URL
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]+\ /test/[^\ ]*\ HTTP/
RewriteRule ^test/([^./]+)\.([^./]+)$ http://localhost/download/$2/$1 [R=301,L]
# Internally rewrite request for /download files to /test subdirectory (with re-formatting)
RewriteRule ^download/([^/]+)/([^/]+)/$ /test/$2.$1 [L]
</IfModule>

Had to adjust the bottom re-write for it function, it was getting ignored otherwise.

It does work though, because i can now do [localhost...] and it grabs the file in [localhost...]

However the top rule is not working, i can type in [localhost...] and i'm still able to read the directory and request the files in there (currently just 1 txt document - while testing).

I know indexes can be turned off but i really like the idea of re-writing the requests as you example shows. I've not got the faintest idea why it won't work though..

I'm testing this locally, so i'm not sure whether that makes a heap of difference or not, but i thought it worth mentioning.

And sorry to sound like a total idiot, but where do i find the "Apache Forum Charter" ?

aceBoB

9:57 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Sorry i didn't mean the rule got ignored before i rewrote part of it, it was ignoring the second part...

So i'd end up with requests going to test/.txt which should be test/myfilename.txt

Just wanted to clarfiy it worked to a degree before the slight changes..

g1smd

10:02 pm on Jul 14, 2009 (gmt 0)

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



Charter: Link is near the top of every page of this forum.

aceBoB

10:44 pm on Jul 14, 2009 (gmt 0)

10+ Year Member



Thanx, it's ok i think i've got there now..
Options -Indexes +FollowSymlinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . http://localhost/ [R=403]
RewriteRule ^download/([^/]+)/([^/]+)/$ /test/$2.$1 [L]
</IfModule>

Forbidden access to the files, and no directory listing, whilst valid requests to download/ext/file work... :)

Some good examples here, from the charter.. :)
[httpd.apache.org...]

It works, so i'm happy... :)