Redirect requests for live image urls and pass the original url value in the redirect

Suppose I have someone (or Google Images) trying to access

http://null.com/uploads/someimage.jpg

(virtually any image in the / uploads folder)

I want htaccess to redirect user to

http://null.com/page/

      

HOWEVER, I need to pass the original url ( http://null.com/uploads/someimage.jpg

) to the redirect target page as a POST string value that the php target page can use to work with it.

Can this be achieved with htaccess?

UPDATE - and it would also be interesting if this only worked when the user agent is a human controlled browser and not a bot like Google Bot, so Google and other search engines can crawl images accordingly without redirecting

+3


source to share


1 answer


You can try this:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php           [NC]
RewriteCond %{REQUEST_URI} ^/uploads/([^/]+)/?  [NC]
RewriteRule .  page.php?url=uploads/%1 [L]

      

The cards are silent

http://null.com/uploads/someimage.jpg

with or without a slash

To:

http://null.com/page.php?url=uploads/someimage.jpg

The string uploads

is considered fixed, and someimage.jpg

can be any name.

script name page.php

- example, can be any name. Replace all instances in the ruleset.

For permanent and visible redirection, replace [L]

with[R=301,L]

This answer matches that description in the OP's comments:

"... redirect the user to this page and pass the old original url as the post value ..."



where page

is script.

NOTE. The parameters passed in the substitution URL can be written using PHP using $_GET

or $_REQUEST

rather than $_POST

.

UPDATE

Here's another version:

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !page\.php        [NC]
RewriteRule ^(.*)\.jpg   page.php?url=$1.jpg [L]

      

The cards are silent

http://null.com/any/number/of/folders/someimage.jpg

To:

http://null.com/page.php?url=any/number/of/folders/someimage.jpg

The rewrite rule is only applied when the incoming URL contains a file with an extension jpg

at the end of the path.

+2


source







All Articles