.htaccess image redirect - php

I've written a PHP class to add a watermark to an image, it works great when accessed via URL directly. I would like to "redirect" every image to my URL like so:
http://www.mysite.com/img.jpg
to this
http://www.mysite.com/watermark/watermark.php?image=http://www.mysite.com/img.jpg
Basically what I want is to pass the src attribute specified in the HTML to the image variable. I'm currently struggling with my .htaccess file and I can't get it working, here it is:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^_].*\.(gif|jpg|png))$ /watermark/watermark.php?image=$1 [L]
Looking forward to your replies and thanks in advance!

Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.(jpg|png|jpeg|gif)$ watermark/watermark.php?image=$1.$2 [NC,L]

Try adding -MultiViews to your options line, so it reads as:
Options +FollowSymLinks -MultiViews

Related

How to have a post variable defined without '?{name}=' in the url?

Currently on my website, my profile urls look like this:
../user/?id=1
I would rather them look like this:
../user/1
How would I go about this? I'm running this with Apache and PHP if that is important to the question.
You can do this with .htaccess
Example:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?id=$1 [L,QSA]
</IfModule>
This htaccess rule will redirect input like www.site.com/user/123 to user.php?id=123

it is possible to add folder prefix in for all images path?

I've a drupal 8 website and it contents contains some url like this
http://localhost/sites/default/files/inline-images/magento.png
this link is come from a block which was saved in db /sites/default/files/inline-images/magento.png
I want to convert all images into http://localhost/folder/sites/default/files/inline-images/magento.png
can i do this by .htaccess mod rewrite?
Did you try something like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^(.*)\.(png|jpg|gif|svg)$ /folder/$1.$2 [L,R=301]
</IfModule>
You can try removing this part R=301 and leave only L.
At the top of your htaccess root add the following :
RewriteEngine on
RewriteRule ^/?folder/sites/default/files/inline-images/(.+\.png)$ /sites/default/files/inline-images/$1 [L,NC]
This will internally redirect /folder/sites/default/files/inline-images/foo.png to /sites/default/files/inline-images/foo.png .

mod_rewrite to serve a new version of existing files

I have just made a central dynamic script to replace a messy folder full of static files. However, I would like to preserve the old URL's without having to delete those files. Let me give you an example:
I have the following files:
/oldpages/projekt1/index.html
/oldpages/projekt2/index.html
/oldpages/projekt3/index.html
The actual new urls are:
/newscript/script.php?name=projekt1
/newscript/script.php?name=projekt2
/newscript/script.php?name=projekt3
The rewrite rule is:
Options -MultiViews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^oldpages/([A-Za-z0-9_\-]+)/(index.html)?$ /newscript/script.php?name=$1 [L,QSA]
However, whenever I try to access http://mydomain.com/oldpages/projekt3/index.html it just keeps giving back the old files. After googling, everyone said that multiviews may be the culprit, but apparently, that's not fixing it.
The rule works if the directory doesn't exist on the server, the following works perfectly:
Options -MultiViews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^newpages/([A-Za-z0-9_\-]+)/(index.html)?$ /newscript/script.php?name=$1 [L,QSA]
And http://mydomain.com/newpages/projekt3/index.html gets the right page.
What am I doing wrong?
Any help would be greatly appreciated.
If a .htaccess in /oldpages has RewriteEngine On, that overrides all rules in the .htaccess in /.

HTAccess bug. GET doesn't seem to come through correctly

I'm using the following htaccess:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^CuraXL/(.*)$ "CuraXL/index.php?pageRequest=$1"
RewriteRule ^CuraXL/(.*)/$ "CuraXL/index.php?pageRequest=$1"
When Doing the following in php:
<?php echo $_GET['pageRequest']; ?>
It outputs "index.php". Instead of what i request being "about-us".
Any idea what's up?
No, CuraXL/index.php is also matched by ^CuraXL/(.*)$. You need to exclude the destination you are rewriting to:
RewriteCond $1 !=index.php
RewriteRule ^CuraXL/(.*)/?$ CuraXL/index.php?pageRequest=$1
Remove the quotes around the index.php bit in the .htaccess

URL Rewriting-Page display - rewrite rule in .htaccess

For URL Rewriting, i have got the output for static URL
But , for dynamic URL , i am getting partial output only.
I have placed the .htaccess file in the root directory only.
Here is the code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^booking/price/([0-9]+)/?$ booking.php?price=$1
What will be the solution ?
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^booking/([0-9]+)/?$ booking.php?price=$1
RewriteRule ^booking/([0-9]+)/([0-9]+)/?$ booking.php?price=$1&pass=$2
You really want to customise your code for these though so as to be able to use general rules, rather than converting specific paths to use $_GET.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^booking/([0-9]+)/?$ booking.php?price=$1
RewriteRule ^booking/([0-9]+)/([0-9]+)/?$ booking.php?price=$1&pass=$2
For images and Css you need to define your base folder in htaccess file as
RewriteBase /~your folder/

Categories