.htaccess redirect from image to article - image

im trying to redirect traffic i get from reddit to an article, but the image in the article is being hot-linked on reddit.com
this is fine but im wondering if theres a way that when users click on the image on reddit.com with a path like:
http://mysite.com/i/12345.jpg to redirect to http://mysite.com/r/12345
the only site I've noticed to do this successfully is livememe.com which has an image on reddit redirect to the article when clicked on. for example:
http://www.livememe.com/36opcf5.jpg redirects to http://www.livememe.com/36opcf5
and im trying to do something similar. Ive noticed that this redirect occurs whenever you go directly to that url.

Try this code :
RewriteEngine On
RewriteRule ^i/([a-zA-Z0-9]+)\.(jpe?g|JPE?G)$ /r/$1 [R=301]

I know it's been a few months since you asked, but if you still need a solution, I found an answer. Tried to find how it is done for a few hours, but unsuccessfully, so I had to come up with my own solution. Livememe doesn't do that redirect with htaccess, they do it with PHP. Since you can't see the difference in referrer, it would be the same whether the picture is embedded, or user clicks a link to it, you have to check something else. And the only difference I see is HTTP_ACCEPT header. If a resource is used as an image, it won't send HTTP_ACCEPT with a value of "text/html", it will be "image/jpeg" or something like that. So what you do is something like this: first, create .htaccess file with this:
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /redirect.php?q=$1 [L]
So if a file is not found, redrect.php is loaded (wile your URL stays the same). And inside redirect.php put something like this:
$accept = explode(',', $_SERVER['HTTP_ACCEPT']);
$file = $_GET['q'];
$file_info = pathinfo( $file );
$url = $file_info['filename'];
$ext = $file_info['extension'];
$check_html = array_search('text/html', $accept);
if ( file_exists("$url.html") ) {
if ($check_html !== FALSE) header("location: http://yourdmain.com/$url"); // location of URL you want to redirect to
else header("location: http://i.yourdomain.com/$url.$ext"); // loction of image file
}
else {
header('HTTP/1.0 404 Not Found');
print 'File not found';
}
Adjust according to your server (the file_exists() part). Or you can check the database, to see that such post exists. Well, I think you get the idea, how that is done. Hope that helps.

Related

Codeigniter 4 advanced image routing issue

I converted the Codeigniter 3 advanced image library to Codeigniter 4 but the Route is not working
Codeigniter 3 route = $route['public/image/(:any)'] = 'media/resize/$1';
Codeigniter 4 route = $routes->get('public/image/(:any)', 'Media::resize/$1');
what is the point?
Solved
Change htaccess:
RewriteCond $1 !^(index.php|public|robots.txt|ads.txt|images|stylesheets|scripts|robots.txt)
to
RewriteCond $1 !^(index.php|robots.txt|ads.txt|images|stylesheets|scripts|robots.txt)

route file issues in codeigniter?

When i write below code in route file it work perfectly but my "admin panel" redirect on frontend page(frontend/login/process link)(not show).
When i comment this, admin panel / frontend both working perfectly but not show this list page.
$route['(:any)'] = "frontend/home/productlist/$1";
I have used others in route file. Is there any issue let me know please.
$route['process'] = "frontend/login/process";
$route['admin']="admin/login";
$route['admin/product/(:num)'] = 'admin/product/index/$1';
$route['(:any)'] = "frontend/home/productlist/$1";
use like this :
$route['/(:any)'] = "frontend/home/productlist/$1";

How to load an index page without a controller

I have a question and apologies if it is too simple but I really couldn't figure it out. If I have my domain for example: www.shop.com. I changed .htaccess to omit the need for index.php. Now I am wondering how can I load for example a page (main home page) without having any controller in url address. E.g.:
www.shop.com =========> should land me on the home page of my site.
currently the only way I can do it is by defining a controller and doing this:
www.shop.com/controller/
Thanks and your help is much appreciated :)
Go to /application/config/routes.php
Add the default_controller rule like so
$route['default_controller'] = 'home';
So now in /application/controllers/home.php method index() will run on the index page.

How do I edit the index page of your domain in CodeIgniter?

So right now I have this in my .htaccess page:
RewriteEngine On
RewriteCond $1 !^(index\.php|images|js|fonts|css|robots\.txt)
RewriteRule ^(.+)$ index.php?$1 [L]
which makes it so that I can access http://www.yourdomain.com/index.php/page by going to http://www.yourdomain.com/page. However what I can't figure out is how to edit the page that shows up at http://www.yourdomain.com.
I've tried creating a controller called index, but this doesn't do anything unless you go to http://www.yourdomain.com/index
Can anyone help with this? Thank you!!!
The default controller that loads when there are no url segments is defined in application/config/routes.php:
From: http://codeigniter.com/user_guide/general/routing.html
Reserved Routes
$route['default_controller'] = 'welcome';
This route indicates which controller class should be loaded if the
URI contains no data, which will be the case when people load your
root URL. In the above example, the "welcome" class would be loaded.
You are encouraged to always have a default route otherwise a 404 page
will appear by default.
If there are no url segments besides the controller itself, by default the index() method of that controller is called (this is always the case, not just in regards to routing). So with this example you would look at the welcome controller and index method and see which view files etc. are being loaded.

CodeIgniter Url Rewrite (language dependent)

So, i have my .htaccess, my controllers, everything is going fine. I added localization, so now i have Portuguese(Default), English and Italian.
I am using the _lang files in the appplication/languages directory, i am using session->userdata('lang') and everything works fine.
My controllers are named with portuguese words, after the top menu. What i'm looking for is:
to rewrite my url, changing the name of the controller, depending on the session->userdata('lang').
Is this even possible? how?
Thank you
So i am trying, as InFog suggested, in the routes file:
if ($this->session->userdata('lang') == 'english') {
$route['novidades/([a-z]+)'] = 'news/$1';
}
but i just get a blank screen when i open the application.
And i've tried it without the if clause, and nothing happens, when i go to
http://localhost/myapp/novidades
the url stays the same
You can solve this using CodeIgniter Routes. You can do it editing the file 'system/application/config/routes.php:
$route['news/([a-z]+)'] = 'noticias/$1';
This way an URL like '/news/run-fools' will be remaped to 'noticias/run-fools'. Now you can have just one controller =)
Good Luck
Override CI_Router to translate the name in the fetch_class() method to change controllers. Override fetch_method() to change methods.

Resources