- This topic is empty.
Viewing 5 posts - 1 through 5 (of 5 total)
Viewing 5 posts - 1 through 5 (of 5 total)
- The forum ‘Other’ is closed to new topics and replies.
The forums ran from 2008-2020 and are now closed and viewable here as an archive.
I want to redirect http://www.something.com/helloworld.pdf to http://www.something.com/index.php?file=helloworld.pdf and then echoing the name of the file in index.php.
So far I got this code:
RewriteEngine on
RewriteRule ([^/.]+)/?.(?i:jpg|gif|png|pdf|zip|rar|doc|docx|mov|ai|psd|swf|key)$ index.php?file=$1 [L]
They only thing it’s missing is the extension of the file on the echo. What am I doing wrong?
They only thing it’s missing is the extension of the file on the echo. What am I doing wrong?
You only passed the first subpattern ($1
) to your rewritten url. The file extension is inside $2
.
Also note that ([^/.]+)/?.
<– this dot matches “any character,” and so might cause patterns to match/not match that you might not otherwise expect. You can do \.
instead to make it a literal “dot.”
So I changed to:
RewriteEngine on
RewriteRule ([^/.]+)/?.(?i:jpg|gif|png|pdf|zip|rar|doc|docx|mov|ai|psd|swf|key)$ index.php?file=$1.$2 [L]
And it’s printing [filename]. what means that the extension is not being saved in $2.
Any idea?
What’s the (?i:
part supposed to do? I’ve never seen that. It seems that you really only need an optional group (i.e., (this|that|other)
) for this task…? That ?
might be causing the pattern to be non-capturing.
Nevermind. I was able to do what I wanted to:
RewriteEngine on
images
RewriteRule ^(.+).jpg$ index.php?file=$1.jpg [L]
RewriteRule ^(.+).gif$ index.php?file=$1.gif [L]
RewriteRule ^(.+).png$ index.php?file=$1.png [L]
docs
RewriteRule ^(.+).pdf$ index.php?file=$1.pdf [L]
RewriteRule ^(.+).doc$ index.php?file=$1.doc [L]
…
I can add only the files I want to behave like this. :)