htaccess rewrite url, add dir and remove query - .htaccess

I have two versions of images on my server, one with and one without a watermark. On my one website I want to have to non-watermarked version. On the websites that hotlink my images I want to have the ones with the watermark.
I want to rewrite the url to serve the watermarked images on those website. The url structure on my website is this:
www.example.com/wp-content/uploads/image-name.jpg?t=1512472796
and I want to change it to:
www.example.com/wp-content/uploads/wm/image-name.jpg
I am kind of stuck here, no idea to do the part that adds the wm dir to the url and removes the query:
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?example.com [NC]
RewriteCond %{QUERY_STRING} ^t=([0-9]*)$
Any help is highly appreciated.

Related

.htaccess allow hotlinking of images with parameter (?something)

I have code in my .htaccess file to prevent hot-linking of images on my site.
What I need is to make it allow hot-linking of images it they have some parameter at the end of URL, for example:
This image should be blocked:
https://my-site.com/my-images/some-image-file-name.jpg
BUT, ALLOW hot-linking if that same image have parameter at the end, for example:
https://my-site.com/my-images/some-image-file-name.jpg?allow_hot_linking
WHY I NEED SOMETHING LIKE THIS?
For example, if image is sent inside html email, it will not be displayed, but if I could make some .htaccess rule which would allow hot-linking of images with parameter I will simply put that parameter at the end of img source tag of image while making html email and it will appear when email is opened.
This is my .htaccess code
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?my-site.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?nnn.nnn.nn.nnn [NC]
##Allow search engines
RewriteCond %{HTTP_USER_AGENT} !Googlebot-Image [NC]
.
.
.
##Allow blank referrers
RewriteCond %{HTTP_REFERER} !^$
## Redirect to hotlink image
RewriteRule ^my-images/(.*).(jpeg|jpg|bmp)$ https://my-site.com/no-hotlinking.html [NC,R,L]
## End - prevent image hotlinking
Love this solution, exactly what I was looking for to allow links from emails but not from blank referrers.
RewriteCond %{QUERY_STRING} !^ahl$ [NC]
Works a dream.

.htaccess: rewriteCond to another page within same URL with params

I've an condition for my .htaccess for crawlers and search engines which takes them to a "static" page where they can scrape all content.
Up until now I've had my domain {client}.realdomain.com where {client} is a subdomain for one client.
When the client then shares something on a social network, e.g. facebook/linkedin their crawlers are taken to my .htaccess which have following conditions (and this works)
URL example: http://{client}.realdomain.com/s/token
RewriteCond %{HTTP_USER_AGENT} (LinkedInBot/[0-9]|facebookexternalhit/[0-9]|Facebot|Twitterbot|twitterbot|Pinterest|pinterest|Google.*snippet|baiduspider|rogerbot|embedly|quora\ link\ preview|showyoubot|outbrain|slackbot|vkShare|W3C_Validator)
RewriteCond %{HTTP_HOST} ^(.+?)\.realdomain\.com$
RewriteRule ^s/(.*)$ http://%1.realdomain.com/static.php?token=$1 [NC,L]
will end up as http://{client}.realdomain.com/static.php?token=token
As said, anything here works perfect but now I'm moving into having different domains, so it can be
{client}.real-domain.com and {client}.sunset.com
I essentially what the same thing in my .htaccess but it should take the whole domain with it when it redirects so it will go to e.g. http://{client}.sunset.com?static.php=token=my-secret-token if a crawler comes to {client}.sunset.com/s/my-secret-token
How would I got about doing this? I seem to be a simple solution but for some reason I just can't seem to get my head around it.
Thanks
RewriteCond %{HTTP_USER_AGENT} (LinkedInBot/[0-9]|facebookexternalhit/[0-9]|Facebot|Twitterbot|twitterbot|Pinterest|pinterest|Google.*snippet|baiduspider|rogerbot|embedly|quora\ link\ preview|showyoubot|outbrain|slackbot|vkShare|W3C_Validator)
RewriteCond %{HTTP_HOST} ^(.+?)\.%{HTTP_HOST}%\.com$
RewriteRule ^s/(.*)$ http://%1.%{HTTP_HOST}%.com/static.php?token=$1 [NC,L]
Can you test this out? By replacing your domain name with %{HTTP_HOST}%

.htaccess redirect all images in image folder

Ok, so I have a small predicament. I have a large image gallery that has over 30,000 images inside of it. I've decided to change to a newer piece of software, and this new gallery is completely different in the way image URLs are handled. So a simple redirect isn't going to cut it anymore (there is no way to determine a connection between A and B, per se, because the old gallery used ID's whereas the new one uses titles for SEO).
However, I would like to still accomodate to the old URL's by redirecting visitors to the corresponding image files. The script that they were contained within maybe gone, but the database and ID's of each image are still there - I was thinking to just rename every image file to the match the ID in the database, and use a redirect rule somehow to link to them.
The old URL structure looked something like this:
domain.com/image/3343/
All of the images are contained within the /images/ folder. If I rename every image filename to match the ID's (as above), how can I use htaccess to redirect the URLs to these files?
Keep in mind.. I need a solution that doesn't involve 30,000 rows in a htaccess file :)
Try:
RedirectMatch 301 ^/image/([0-9]+)/$ /image$1.jpg
Or using mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^image/([0-9]+)/$ /image/$1.jpg [L,R=301]
For inside the image folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/$ /image/$1.jpg [L,R=301]

htaccess seo friendly urls for all subfolders

i have script in index.php in ALL folders
need to make pretty url/seo friendly urls like this: site.com/folder1/something.html
now i have ugly urls like this: site.com/folder1/index.php?category=something
this part index.php?category=something is always the same in all subfolders, i just want to change it to more seo friendly like something.html
once again: find index.php?category=1$ and replace it with 1$.html
do not touch anything else, just this part of url
so when i visit:
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something
need to see this in address bar:
site.com/another-subfolder/and-one-more-folder-here/something.html
i hope you get it?
i tried this with folder1 in htaccess in root
RewriteRule ^folder1/(.*).html$ folder1/index.php?category=$1 [L,R=301]
ok this works, but how can i make this to work for all subfolders accross the site like this:
site.com/folder145/index.php?category=something
site.com/subfolder/index.php?category=something
site.com/another-subfolder/and-one-more-folder-here/index.php?category=something
there will be 1000s of subfolders with different names so manually making RewriteRule for each subfolder won't work
any help is appreciated
First, you need to change all the links that you generate from the index.php?category= form to the category.html form.
Then in the htaccess file in your document root, you add these rules to internally rewrite the category.html form back to the index.php?category= form:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?/)?([^/]+)\.html$ /$1index.php?category=$2 [L]
Then, to point links you don't have any control over (or ones generated using a FORM) externally redirect any direct access to links in the index.php?category= form:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ (/.*?)?/index\.php\?category=([^&\ ]+)
RewriteRule ^(.+?/)?index\.php$ /$1%3.html? [L,R=301]
This should match any and every subdirectory.

.htaccess image to web page redirect?

Is there a .htaccess script I can use to redirect the url of an image to an actual web page?
What can I do to make it so when someone accesses an image via their address bar by typing in: http://www.sitename.com/images/1.jpg
It will instead redirect the user to the web page:
http://www.sitename.com/view/1.html
I want to still be able to place the images in image tags though.
RewriteCond %{HTTP_REFERER} !^https?:\\www\.yourdomain\.com
RewriteCond %{REQUEST_URI} (.*)\.jpg$
RewriteRule images/(\d+)\.jpg http://www.yourdomain.com/view/$1.html [L,R=301]
by using this you can put images in img tag and it will work but If you try to access them directly , it will redirect to html file.
Note: If you want others have permission to use your images use this:
RewriteCond %{HTTP_REFERER} ^$

Resources