I'm wondering if there is a rule I can add to my .htaccess file that will make the URL of an image to appear as another image URL. Here is the use case I'm referring to:
My app automatically watermarks images using the GD library on the fly. In turn, the newly watermarked image has an image source of localhost/some/dir/image.php?original=someFilename.png.
Is it possible to make that dynamic image URL appear as the original, but still serve the dynamic, watermarked image? Thanks in advance.
This should work (currently testing it):
RewriteRule ^(.+\.png)$ /some/dir/image.php?original=$1
Works for me, but you probably do not want to allow any characters in the filename (".+").
The following would be much more restrictive but also safer:
RewriteRule ^([A-z0-9]+\.png)$ /some/dir/image.php?original=$1
Related
I have these three links:
localhost/my_projects/my_website.php
localhost/my_projects/my_website.html
localhost/my_projects/my_website
The paths of the php and html files are as follows:
C:\xampp\htdocs\my_projects\my_website.php
C:\xampp\htdocs\my_projects\my_website.html
The link without an extension is "artificial" and I want to use said link:
localhost/my_projects/my_website
to get the contents of either of these links:
localhost/my_projects/my_website.php
localhost/my_projects/my_website.html
The reason for the two example files, instead of just one, is that I want to be able to switch between those two files when I edit the htaccess file. Obviously I only want to access one of those files at a time.
What do I need to have in my .htaccess file inside the my_projects folder to accomplish that? How can I make one specific link redirect to another specific link?
After reading your comment clarifying your folder structure I corrected the RewriteRule. (By the way, it would be best if you add that info to the question itself instead of in comments).
The url you want to target is: http://localhost/my_projects/my_website
http:// is the protocol
localhost is your domain (it could also be 127.0.0.1 or a domian name like www.example.com in the Internet)
I assume you are running Apache on port 80, otherwise in the url you need to also specify the port. For port 8086 for example it would be http://localhost:8086/my_projects/my_website.
The real path is htdocs/my_projects/my_website.php or htdocs/my_projects/my_website.html depending on your needs (obviously both won't work at the same time).
Here the my_projects in the "fake" url collides with the real folder "my_projects" so Apache will go for the folder and see there is no my_website (with no extension) document there (it won't reach the rewrite rules).
There is a question in SO that provides a work around for this, but it is not a perfect solution, it has edge cases where the url will still fail or make other urls fail. I had posted it yesterday, but I seem not to find it now.
The simple solution if you have the flexibility for doing it is to change the "fake" url for it not to collide with the real path.
One option is for example to replace the underscores with hyphens.
Then you would access the page as http://localhost/my-projects/my-website if you want to keep a sort of "fake" folder structure in the url. Otherwise you could simply use http://localhost/my-website.
Here are both alternatives:
# This is for the directory not to be shown. You can remove it if you don't mind that happening.
Options -Indexes
RewriteEngine On
#Rule for http://localhost/my-projects/my-website
RewriteRule ^my-projects/my-website(.+)?$ my_projects/my_website.php$1 [NC,L]
#Rule for http://localhost/my-website
RewriteRule ^my-website(.+)?$ my_projects/my_website.php$1 [NC,L]
(Don't use both, just choose one of these two, or use them to adapt it to your needs)
The first part the rewrite rule is the regular expression for your "fake" url, the second part is the relative path of your real folder structure upto the page you want to show.
In the regular expression we capture whatever what we assume to be possible query parameters after .../my_website, and paste it after my_website.php in the second part of the rule (the $1).
Later on if you want to point the url to my_website.html, you have to change the second part of the rule, where it says .php, replace it by .html.
By the way, it is perfectly valid and you'll see it in most SEO friendly web sites to write an url as http://www.somesite.com/some-page-locator, and have a rewrite rule that translates that url to a page on the website, which is what I had written in my first answer.
I'm putting together a simple image beacon for an affiliate network, which I will use to track when the script is loaded on the affiliate sites. Rather than give them code with a PHP script as the source for the image (which makes some users nervous), I want to serve a "simple" gif in the image source tag. However, I want to name the image off of their affiliate ID, then pass that to my script.
So, I want them to have a tag that looks like this:
<img src = "https://example.com/images/aff1234.gif">
But I want mod_rewrite to actually serve them:
https://example.com/my_tracking_script.php
I have the script working for my_tracking_script.php - it creates a 1x1 gif file with all of the proper headers and cache options, it enters the tracking info into my db, etc. But the PHP expects to get some sort of variable. If I could access, say, $_GET['affimage'] and set that to aff1234.gif, I can strip the text out and have affiliate ID of 1234 for my script.
But I suck at mod_rewrite, and while I've seen similar questions, I can't find the exact syntax I need.
The following htaccess code should work:
RewriteEngine On
RewriteRule ^images/([^/]+\.gif)$ /my_tracking_script.php?affimage=$1 [L]
Keep in mind, that mod-rewrite needs to be enabled for the above to work.
I have a group of images that should be progressively made available on specific dates in the future until the whole set is visible. The images have the date they should be visible as the file name. Anyone with a few insights into the workings of the internet could figure out the pattern in the file names and look at 'future' images before the intended date.
I'm hoping to solve this with .htaccess to prevent spinning up a script every time an image is accessed. Do you think it's possible?
The locale of the server is the only one I'm concerned about.
Thanks :)
Edit:
As an example, at the time of posting this message I'd want this image to serve correctly:
http://domain.com/images/2012-11-20.jpg
But I'd want this one to return a 404, 401 or whatever as it's in the future:
http://domain.com/images/2012-12-06.jpg
I don't seem to fully understand your question, but this link about time-dependant rewriting might give you a little push towards the right direction.
http://www.askapache.com/htaccess/time_hour-rewritecond-time.html
(I am in no way affiliated with this website!)
Rather than having the code change the src of the image, you could dynamically rewrite a fixed image source to the correct daily location:
RewriteCond %{REQUEST_URI} /my/image.jpg
RewriteRule (.*) /images/date/%{TIME_YEAR}-%{TIME_MON}-%{TIME_DAY}.jpg [L]
It's a bit security-by-obscurity, and you lose the ability to long-term cache the image (because come the next day, you'll want the browser to load the new image from the old path), but it might fit your purpose.
I have third party sites that link to some images on my site. The images were placed in Magento's image cache some time ago. But when the cache is refreshed, Magento modifies the file names and thus the links become unreachable. It is not every image just certain ones that this is happening to. I have 22 images where I need to do this.
How can I modify my .htaccess to make the links go to a static copy of the image located in another directory?
Take a look at mod_alias and RedirectMatch, you can use regular expressions to match against a URI and a target (where to redirect to), if you don't need regular expressions, you can just use Redirect.
RedirectMatch /old_image_uri /new_image_uri
Hopefully someone here can point me in the right direction as htaccess is driving me crazy at the moment.
What i am trying to achieve is an automatic redirect for certain images
Currently I use jquery to replace the image src.
The reason for this is the new resized images are in a different directory.
The problem with this method is every time we refresh we have to wait for the dom to fully load.
And I see this is possible with htaccess.
Redirect /my-domain.com/images/image1.png /my-domain.com/images/resized/image1.png
Currently this works, but for over 100 images I really need to find a dynamic solution for this
I tried the following which obviously failed.
RewriteRule ^/my-domain.com/images/(.*) /my-domain.com/images/resized/(.*) [R=301,L]
the resized directory has several directory's so the rule needs to apply to all child directories.
Although it's not a big problem to list all the directories as long as I don't list all images.
hopefully I am missing something simple here, also I wanted to make sure the redirect will not effect SEO?
maybe there is an alternative solution with htaccess?
This is a bit of a messy way to handle images - across multiple folders - but, if that's how you want to manage it, fair enough.
From the above, I understand that:
There are some images within the /images/resized/ folder
There are also some images within subfolders of the same
You want to be able to call a URL within the /images/ folder and have it transcribed to the /images/resize/ folder (with the same end)
In the webroot's .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/images/
RewriteCond %{REQUEST_URI} !^/images/resized/
RewriteRule ^images/(.+)$ /images/resized/$1
Tested OK with this htaccess tester.
"I wanted to make sure the redirect will not effect SEO?"
Filenames are not as important for SEO as alt tags and titles. There should be no change to the SEO stance of your site as a result of this change.