Forum Moderators: phranque
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
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]
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]
# 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]
Jim
<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" ?
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... :)