Image prefix (url rewrite) - preg-match

I'm use this tutorials to add watermark on my images:
http://www.reviewsforjoomla.com/forum/index.php?topic=15067.0
All JPG are correctly marked up with this htaccess rule:
RewriteEngine on
RewriteRule ^([^tn].*\.(jpg))$ watermark.php?image=$1&watermark=watermark.png [NC]
Now, i need to apply different watermarks based on file prefix, like:
AA-myimage.jpg (apply aa-watermark.png to all files with AA- prefix)
BB-myimage.jpg (apply bb-watermark.png to all images with BB- prefix)
There is a way to add this on htaccess?
Thanks all!

Try this rule:
RewriteRule ^([a-z]{2})-(.+?\.jpg)$ watermark.php?image=$1-$2&watermark=$1-watermark.png [NC,L]

Related

mod_rewrite: transform = and & in slashes

I was just looking for a solution to transform any =,?,& found in a query string into a simple slash /.
To be more specific, my link is something like:
http://www.mydomain.com/product.php?c=1&sc=12&products_id=15
and I would it like this:
http://www.mydomain.com/product.php/c/1/sc/12/products_id/15
whatever the master page could be (in this case is product.php, but it could be foo.php, bar.php...or else).
I have googled a lot but didn't find any good solution to achieve what i'm looking for.
I have found complex rewrite rules, but they all include the "page name" into them:
i.e.
RewriteRule ^/?index/([^/]+)/([^/]+)$ /index.php?foo=$1&bar=$2 [L,QSA]
That rule is only applicable to index.php and to known variables like foo, bar.
I need a more general one, whatever the master page is, whatever the variables are.
Can this be done?
Any suggestion?
Thanks
I assume you're using apache >= 2.2. Add this to your apache conf:
<IfModule mod_rewrite.c>
RewriteEngine On
# you absolutely need to use RewriteBase if this snippet is in .htaccess
# if the .htaccess file is located in a subdirectory, use
# RewriteBase /path/to/subdir
RewriteBase /
RewriteCond %{QUERY_STRING} ^(=|&)*([^=&]+)(=|&)?(.*?)=*$
RewriteRule ^(.*)$ $1/%2?%4= [N,NE]
RewriteCond %{QUERY_STRING} ^=$
RewriteRule ^(.*)$ $1? [R,L]
</IfModule>
The first RewriteCond/RewriteRule pair repeatedly matches a token delimited by & or = and adds it to the path. The important flag is the [N] that causes the whole ruleset to start over again, as often as the rule matches. Additionally, a = is appended to the end of the query string. This is to create a mark in the URL that at least one rewrite has happened.
The second ruleset checks for the = mark that remains after the URL has been rewritten completely and issues a redirect.
Have a look at http://wiki.apache.org/httpd/RewriteQueryString for some useful hints.

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

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

.htaccess RewriteRule for change the path if a word appears in the path

:-)
I need a .htaccess RewriteRule that can do this:
If the browser load this url path:
http://www.domain.com/example/word_to_change/
change to
http://www.domain.com/example/new_word/
take a look that the "word_to_change" and "new_word" appears in the third level in the path
I was tried this:
RewriteRule ^(word_to_change/)?$ /new_word/ [R,L]
But only works if the "word_to_change" appears in the second level, not in the third.
thanks for your help! :)
ADDED:
three examples:
need to change
http://www.domain.com/second_level_with_any_word/specific_word_1 or
http://www.domain.com/example_1/specific_word_1 or
http://www.domain.com/example_2/specific_word_1
to
http://www.domain.com/second_level_anything/specific_word_2 or
http://www.domain.com/example_1/specific_word_2 or
http://www.domain.com/example_2/specific_word_2
maybe is mor simple for explain with this other example:
http://*/*/specific_word1
for
http://*/*/specific_word2
This depends on what to acomplish, if you want to change ANY word that apeaers after example with a SPECIFIC word then what you need is:
RewriteRule ^example/(.*) /new_path/ [R,L]
if you want to pass that word as a paramater you can use
RewriteRule ^example/(.*) /new_path/process.php?word=$1 [R,L]
if you want to send any URL that ends with a specific word try this
RewriteRule ^(.*)/old_word/$ /$1/new_word/ [R,L]

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]

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