htaccess rewrite all image requests from a non-existent folder to folder-name.jpg - image

Here's what I am trying to do, for example . . .
http://www.website.com/images/folder-with-a-crazy-name/any-image-at-all.jpg
would rewrite to . . .
http://www.website.com/images/folder-with-a-crazy-name.jpg
Does this make sense?
Any help would be appreciated.

To rewrite all jpeg image files in a folder to that folder with .jpg appended
RewriteRule ^images/(.+?)/[^/]+?\.jpg$ /images/$1.jpg [L]

Just paste this into your .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^images/([a-z0-9-_]+)/any-image-at-all.jpg /images/$1.jpg
Or if you want even the other image formats to be rewritten, then just replace the third and the last line on the code above with this rule:
RewriteRule ^images/([a-z0-9-_]+)/any-image-at-all.([a-z0-9]{3}|[a-z0-9]{4}) /images/$1.$2
If the the folder images is also a variable, then try one of the following rules below:
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/any-image-at-all.jpg /$1/$2.jpg
If the file extension is a variable, then try this:
RewriteRule ^([a-z0-9-_]+)/([a-z0-9-_]+)/any-image-at-all.([a-z0-9]{3}|[a-z0-9]{4}) /$1/$2.$3

Related

htaccess rewrite for clean image url

I'm looking for a re-write for an identicon generator where something like
images/1-2.png
Would be interpretted on the server as
images/index.php?one=1&two=2
But it would still show site.com/images/1-2.png in the address bar.
Assumed there's only one dash (-) in the file name
Doable?
This will do it, put this in your .htaccess file in the root of your application.
RewriteEngine on
RewriteBase /
RewriteRule ^images/(\d)-(\d)\.png images/index.php?one=$1&two=$2 [NC,L]

mod_rewrite to parent directory

I would like to create a .htaccess file that would do this logic:
If the requested file is not found try to find it in the directory
above.
I don't want to redirect the browser I would just like to internally rewrite the request.
I tried and searched for this a lot but always got stuck because (as I gather from the log) the where I could do the rewrite the path was always already without its per directory prefix. In the example below the .htaccess file is in the lang folder. If the lang specific file is not found it should just take the file from the parent folder. I understand that it is possible to do it by hardcoding the parent directory or by placing the .htaccess higher, but now that I suffered for so long in trying I would be very interested to learn if it was possible at all this way.
strip per-dir prefix: X:/localhost/htdocs/peopletest/public/img/root_cli/lang/en/loginhead.gif -> en/loginhead.gif
applying pattern 'somePattern' to uri 'en/loginhead.gif'
Thanks for the help.
SWK
Like this?
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-l
RewriteRule /[^/+]/([^/]+)$ $1

mod_rewrite preventing correct filetype icons from showing on mod_autoindex

I seem to have a problem which I've been struggling to solve. I'd love a bit of help here.
I have a dump site where I am using mod_autoindex and FancyIndexing.
I have my FancyIndexing icons defined in my .htaccess file. I also use mod_rewrite to pass the files through several php scripts (depending on the file extension).
Everything works great, except when a file extension is matched on a RewriteRule, Apache displays only it's default icon, and not the one specified.
All FancyIndexing resources are on the /imgindex directory, and all viewers are on the /viewers directory.
This is the relevant part of the .htaccess file (I'm currently tweaking it, so it's not optimized):
Options +Indexes
IndexOptions +XHTML +HTMLTable +FancyIndexing +FoldersFirst +SuppressHTMLPreamble +IconsAreLinks +IgnoreCase +NameWidth=*
IndexIgnore *~ imgindex viewers favicon.ico
HeaderName /imgindex/header.html
ReadmeName /imgindex/footer.html
# ------ Fancy Indexing ----------
AddIcon /imgindex/image.png .jpg .jp2 .jif .jpeg .tiff .tif .pict .pct .bmp .gif .png .psd .tga .ai .indd .fh* .fh10 .xcf .svg
AddIcon /imgindex/app.png .app
AddIcon /imgindex/movie.png .mov .mpg .mpeg .m2v .avi .divx .xvid .swf .wmv .wma .wm* .ram .rm .ogm .ogv
AddIcon /imgindex/txt.png .txt .text .log
# etc, etc.
DefaultIcon /imgindex/text.png
# ------ Rewriting --------
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^viewers/(.*)$
RewriteCond %{REQUEST_URI} !^imgindex/(.*)$
RewriteRule ^(.*).(css|cs|cpp|h|hpp|pas|xml|js|asm|inc|as|sh|bat|cmd|html)$ /viewers/view_source.php?file=$1.$2&%{QUERY_STRING} [NC,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^viewers/(.*)$
RewriteCond %{REQUEST_URI} !^imgindex/(.*)$
RewriteRule ^(.*).(jpg)$ /viewers/viewjpg.php?file=$1.$2&%{QUERY_STRING} [NC,L]
# (etc, etc.)
As I said, everything seems to work great, except for the icons. In the case above, all .css, .cs, .cpp, (etc), and all .jpg files will use the icon defined in DefaultIcon, instead of the one for its filetype (defined in AddIcon).
If I remove the RewriteRule that matches that filetype, the correct icon gets displayed.
Is this normal, expected behaviour? Any way to override it, if so?
Regards,
Ok, seems I found out why. mod_rewrite is, for some reason (a bug?) remapping all apr_read_dir() calls, which mod_autoindex uses (I ended up looking at mod_autoindex source code). Since it's considered a subrequest on the mapper module, as a workaround I just added a:
RewriteCond %{IS_SUBREQ} false
To each of the RewriteRules and it magically worked. Of course, this is just a workaround, since if you actually need a rewriterule on a subrequest, this might not work for you.
I do believe this to be a bug on mod_autoindex, since there's absolutely no reason the filename for the icon and/or description should be ANY different than the one used to actually print the output data.
There could be some obscure reasons to it though.

Re-write userprofile URL (remove.html)

Any Ideas how to change remove .html in this mod-rewrite script
Doesnt work if I remove ".html"
RewriteRule ^([^/]*)\.html$ /userprofile.php?member_id=$1 [L]
Works as
http://site.com/12.html
but wants to have it as
http://site.com/12
Thank you
To make the .html optional, put it in a group and use the ? quantifier:
RewriteRule ^([^/]*)(\.html)?$ /userprofile.php?member_id=$1 [L]
But as this pattern will now also match any single path segment, you should make it more specific to only match your specific URL path pattern. In this case \d+ instead of [^/]* would be a better choice:
RewriteRule ^(\d+)(\.html)?$ /userprofile.php?member_id=$1 [L]
MODIFIED:
I have tested the following and they work on a CentOS server running Apache.
For directory rewriting:
RewriteRule ^/test/(.*)$ http://www.google.com [L,R]
That will redirect http://www.site.com/test to http://www.google.com.
For all files in a directory this will work:
RewriteRule ^/test/([^/]*)(.*)$ http://www.google.com [L,R]
That will redirect www.site.com/test/12.html or www.site.com/test/298.aspx or any other file in the "test" directory to www.google.com.
So this may be more what you are looking for:
RewriteRule ^/12/([^/]*)(.*)$ /userprofile.php?member_id=$1 [L,R]
ORIGINAL POST:
I believe this is what you are looking for:
RewriteRule ^([^/]*)(.*)$ /userprofile.php?member_id=$1 [L]
That is if you are trying to do the rewrite for files...
It will be slightly different if you want to do the rewrite against a directory.
You need to remove the backslash as well ("\.html").

RewriteCond and Alias

I have defined alias that looks like this:
Alias /pictures/sm/ /var/www/my_site/data/_active_thumbnails/
Later in the VirtualHost section have:
DocumentRoot /var/www/my_site/sites/www.my_site.com/htdocs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/thumbnails/(.*)\.(jpg|JPG) /images/stg-list-img.png [PT,L]
What I'm trying to do is to display /images/stg-list-img.png placeholder image only if the original image does not exist on the drive.
Right now it's replacing all the images from /thumbnails/. It looks like the RewriteCond is not aware about the Alias. Is there the way to overcome it?
Thanks
REQUEST_FILENAME is only the full filesystem path wnen you use it with your rules in htaccess or -- in per-virtualhost config like you have it's still just the URI.
This is mainly because Apache hasn't yet had a chance to map it to any file at this stage.
You could just add the prefix to your -f test, or all of: put your rules in , adding a Rewritebase /pictures/sm/, and changing your rule's regex...
However, your regex doesn't currently make any sense. If the Alias matters and is /pictures/sm, the rewriterule could never match with ^/thumbnails.

Resources