Is it possible to redirect with .htaccess based on a date in the URL for static files? - .htaccess

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.

Related

Remove directories from URL using .htaccess

I've been searching around and tried many different ideas to fix the state of a URL for my client, but have had no joy.
The URL currently looks like this:
website.com/folder1/folder2/folder3/page.php
My client wants this to be showing much like the other links on the website as so:
website.com/page
I'm fine with removing the .php that's simple enough and is already written into my .htaccess file. It's just removing the 3 directories from the URL is what I cannot work out.
Before anyone asks about moving the file to a higher directory, I'm afraid this cannot be done as everything needs to be down in this 3rd directory for this page.
I believe my latest attempt is about the closest I've got to solving this, if anyone could point out my mistakes and a solution it would be much appreciated:
RewriteRule ^/folder1/folder2/folder3/(.*)$ /page$1 [L,R=301]
EDIT:
After researching more today, I think I may have confused people with this question. I think what I should have said is that I need to mask the URL, hiding the 3 directories, and showing just the domain and the page itself.
I still seem to be hitting a wall with masking too. Any advice?
After a whole weekend (and quite a bit of the week between working) I finally managed to succeed with what I wanted.
The site now displays as required:
website.com/page
Which has been redirected and masked from:
website.com/folder1/folder2/folder3/page.php
The piece of code required in the .htaccess is as follows:
RewriteRule ^page$ /folder1/folder2/folder3/page.php
Looks like I was being stupid before and had the syntax backwards, but all is well now and I'm allowed to sleep :)
Hope this helps anyone else in future with this sort of problem!

.htaccess redirect to subfolder, and remove it's name

I'm kind of noob in the world of web so my apologies... I tried many things found on SO and elsewhere, but I didn't manage to do what I want. And the Apache documentation is... well too much complete.
Basically what I want to do is redirect my domain to a subfolder. I found easy solutions for this (many different actually).
http://www.foo.com/
http://foo.com/
should redirect to /bar and appear as http://foo.com/
Using the following I got the expected result :
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^www\.foo.com$
RewriteRule ^/?$ "http\:\/\/foo.com" [R=301,L]
RewriteRule ^((?!bar/).*)$ bar/$1 [NC,L]
But I also want the subfolder as well as filenames not to appear when explicitly entered, i.e :
http://www.foo.com/index.html
http://foo.com/index.html
http://wwww.foo.com/bar
http://foo.com/bar
http://wwww.foo.com/bar/index.html
http://foo.com/bar/index.html
Should all appear as
http://foo.com/
Is this possible ?
Obviously using .htaccess, since I'm on a virtual host.
Thanks
As Felipe says, it's not really possible, because you lose information when you do that R=301 redirect: a hard redirect like this starts a whole new request, with no memory of the previous request.
Of course, there are ways to do similar things. The easiest is to put the original request in the query string (here's a good rundown on how mod_rewrite works with query strings). Sure, the query string does show up in the URL, but most modern browsers hide the query string in the address bar, so if your goal is aesthethics, then this method would be workable.
If you really don't want to show any of the original query in the URL, you might use cookies by employing the CO flag (here are some very good examples about cookie manipulation). At any rate, the information about the original request must somehow be passed in the hard redirect.
But anyhow, and most importantly, why would you want to do something like this? It's bound to confuse humans and robots alike. Great many pages behaved like this back when frames were fashionable, and it was pretty terrible (no bookmarking, no easy linking to content, Google results with the snippet "your browser cannot handle frames", no reloading, erratic back button, oh boy, those were the days).
Speaking of which, if your content is html, you may just use a plain old iframe to achieve the effect (but I'd sincerely advise against it).

Removing forward slashes after a particular subfolder section in a URL address

I have a networking site that has member data organised into subfolders. To access the data, a user would need to go to something like:-
www.domain.com/members/so/me/on/esfolder
My question is, can I use a function in the .htaccess file to allow users to visit the same page by just typing:-
www.domain.com/members/someonesfolder
I have read up on the Rewrite syntax but struggling to understand how to do this. It is simply a case of removing the forward slashes after a particular point.
Any ideas? Thanks in advance.
I've figured it out myself after a non-stop mess around lasting several hours... For those who might want to know, here is the code I used...
RewriteRule ^members/([A-Za-z0-9]{1,2})([A-Za-z0-9]{1,2})([A-Za-z0-9]{1,}) members/$1/$2/$3/ [L]
In the above, there is just three sub-folder drill downs (which is what I have decided to do rather than the four in my original question).
Hope this helps someone else!

Erase '#' from url

UPDATED
I didn't understand how to make this work so I hope you know because I didn't found more solutions:
I have implemented jquery cycle in wordpress with the window.location.hash to make an individual url from each slide (example here http://jquery.malsup.com/cycle/perma.html)
Now I have an indez with some selected photos of differents categories that works with hashes like localhost/prueba/#men/#work61, localhost/prueba/#women/#work15, etc...the trick is that I have also the category /men/ and the photo #work61 permanetly cause the index may change but not the photo in the category indeed. So what I need is change the url without reloading from /#men/ to /men/ and if they share a link they will always go to the right photo and section (did I explain it well ?)
I have been testing many ways to arrive, with history.js and even with the aisle Pushestate but I didn't found the right solution. Lately I have made this with htaccess and it's closely what I need
Options +FollowSymLinks
RewriteEngine On
RewriteBase /prueba/
RewriteRule ^([^/]+)/([^/]+)/$ $1/#$2 [NE,R]
This changes me from localhost/prueba/men/work61 to localhost/prueba/men/#work61 but if I tried to add a hash to the first folder (#men), an also only work if somebody put the link directly
Any idea? Thanks in advance
I think you need to understand the URI specification, section 3.5 https://www.rfc-editor.org/rfc/rfc3986#section-3.5. Essentially, in the context of a web browser displaying HTML documents, the URI fragment (after the #) refers to a subset of the resource (page).
Basically if you removed the # from your URL, you will not be referring to a different subset of the same resource, but another resource entirely. So another page will be requested.

Htaccess Image redirect to specific resize directory

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.

Resources