Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
One thing I've noticed on Facebook is when you access an image/video directly, you can view it, even if the uploader specifies that you're not able to see it.
So I was thinking. Is it possible to redirect any image that is accessed directly (eg. www.example.com/img.jpg) to a page that has that image on? So say you were to access www.example.com/img.jpg, it'd redirect to a specific page for that image (perhaps the page is named after the image or something, so like it'd redirect to www.example.com/img.php).
I imagine it could possibly be done with .htacces, but I'm not advanced with that.
Anyone ever achieved this before and know if it's possible?
Something like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule ^(.*)\.(jpe?g|gif|png)$ /$1.php [L,R]
redirects any request for an image that isn't referred to by your site (www.example.com) to the image's name but with a .php extension instead.
Of course, this only works if you have a php file named after each of your images. If you want just a single script to process all your images (might be easier if you have a lot of images), you can do something like this:
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(www\.)?example\.com/ [NC]
RewriteRule ^(.*\.(jpe?g|gif|png))$ /image_page.php?image=$1 [L,R]
And you'll have a script image_page.php take the $_GET[] parameter "image". And you can output a page that links to the image.
If you'd rather not redirect the browser, just remove the ,R flag inside the square brackets.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
For a project, I need to make every directory (server.com/dir1, server.com/dir2) look like there is the directory, even when there is not. I could say I need a directory wildcard. Say the content would be somewhere on the server, and gets included in the directories. Is there a way to achieve this with .htaccess?
Thanks!
Try something like this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)/*$ ./yourActualScript.php?directory=$1
The regex part in brackets is your folder, the slash star is the rest of the url (if any), and the request will be sent to an actual script with GET var directory
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a hosted Apache based website. Currently, we create a new project folder on the site, and dump the flash based course up for review. Is there a way to make the content folder expire after a certain amount of time so that the content is no longer view-able?
Please let me know if you need any further clarification.
Yes, you can, using mod_rewrite module.
For example, to deny all requests since November 2014 you could use these rules:
RewriteCond %{TIME_YEAR} =2014
RewriteCond %{TIME_MON} >10
RewriteRule .* /deny.html
RewriteCond %{TIME_YEAR} >2014
RewriteRule .* /deny.html
To see all the available environment variables check the link below.
PS: you obviously need to provide some nice /deny.html page so that it was obvious for visitor what happened.
Or if you just need a standard 404/403 use the following rule:
RewriteRule .* - [R=404]
References:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to build a simple URL shortner and I have come across a few complications:
A) in .htaccess how would I rewrite mydomain.com to make it so users can enter mydomain.com/4854 while the page is actually mydomain.com/url.php?key=4854.
B) If I do have that Rewrite rule how can I be sure to bypass it for index.php and add.php
Thanks so much in advanced.
In your .htaccess you can do:
RewriteEngine On
RewriteRule ^([0-9]+)(/)?$ /url.php?key=$1 [QSA,L]
This rewrites requests that contain only numbers to url.php.
Then in url.php, if key is not found, redirect to error page.
RewriteEngine on
RewriteRule ^([0-9]+)(/)?$ url.php?key=$1
And if the key is invalid, redirect in the php file
Let me save you the grief...I was doing the same thing, and found it written for me at yourls.org. It covers pretty much everything you need, as well as giving you option user pages to add their own, AND gives you an API interface for others to use the shortened URLS. I had it up and running in minutes, and spent a couple of hours modifying the samples given to be ready for production.
Even if you don't use it, the mod_rewrite solution is there for you.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I'm trying to remove a PHP Get query from my domain. For example, instead of showing example.com/?url=1234, I'd like it to rewrite to example.com/1234, hiding the query but not removing it. I know this is possible and have read many tutorials on how to do this, but my code just isn't working. Here's what I'm currently trying:
RewriteEngine On
RewriteCond %{QUERY_STRING} url=
RewriteRule (.*) http://example.com/$1? [R=301]
What this is doing is stripping the query entirely, instead of just removing the ?url= segment.
You are thinking about it the wrong way round. Rewriting is not something that the client sees, but something that are exclusive to the server.
This means that you can make example.com/1234 work as though the client had used example.com?url=1234.
To achieve this you would use the following lines:
RewriteEngine On
RewriteRule (.*) http://example.com/url=$1 [QSA]
Check this - htaccess rewrite for query string
and
Htaccess Querystring rewrite
You need to extract the relevant part of the query string in the RewriteCond line.
RewriteEngine On
RewriteCond %{QUERY_STRING} url=(.*)(&|$)
RewriteRule (.*) http://example.com/$1%1? [R=301]
The above will discard any other query string parameters that you give (see examples below).
It will also keep the filename if given.
Examples:
http://example.com/?url=1234 ----> http://example.com/1234
http://example.com/a/?url=1234 ----> http://example.com/a/1234
http://example.com/?url=1234&a=b ----> http://example.com/1234
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to build an online Malayalam to English and English to Malayalam dictionary.
There are two online dictionaries available, however it is not perfect so I have plan to build a good dictionary.
When I check some of the website both of them are used get method at the same time URL details are totally different method. Let me show how it working:
At the same time my website showing like this:
Is there any option to my URL like other two websites? I think both websites are using PHP with jQuery.
try this
RewriteEngine On
RewriteBase /
# make pretty urls work
RewriteRule ^dictionary/([^/]*)$ index.php?ml=$1 [L]
# redirect none-pretty urls
RewriteCond %{QUERY_STRING} ml=(.*)
RewriteRule ^$ /dictionary/%1 [L,R=302]
You'll also need some Javascript to catch the form's onsubmit, and change the url to not have the get parameters. You could do without, but that would result in an extra 302 request, and slow the pageload down a bit.
(PS make sure you php script return a 404 page if it can't find the word in the database.)
You need to use the mod_rewrite from apache2, it's a way for mask your parameters in a user friendly url.
You can read about thins in this tutorial:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
full docs: http://httpd.apache.org/docs/2.2/rewrite/