Create a redirect from the attachments directory to index.php with the slug as a url parameter










0















When a user directly accesses the uploads directory I need to be able to redirect the user to index.php?file=$1. For example:



someurl.com/uploads/sites/17/2018/10/image.jpg


to



someurl.com/index.php?file=/sites/17/2018/10/image.jpg


From this url I will be able to check against the current logged in users meta.










share|improve this question
























  • Do you want to prevent direct access to the entire uploads directory or just images (or other files)?

    – Jamie_D
    Nov 15 '18 at 17:03











  • @Jamie_D I think the rule needs to be like: have we directly visited a file grab the part after the word upload and add it to the query string. Once there I can run a function to check user permissions.

    – James Glendenning
    Nov 15 '18 at 17:11











  • Updated answer for accessing user info and file parmeters ...

    – Jamie_D
    Nov 15 '18 at 18:15















0















When a user directly accesses the uploads directory I need to be able to redirect the user to index.php?file=$1. For example:



someurl.com/uploads/sites/17/2018/10/image.jpg


to



someurl.com/index.php?file=/sites/17/2018/10/image.jpg


From this url I will be able to check against the current logged in users meta.










share|improve this question
























  • Do you want to prevent direct access to the entire uploads directory or just images (or other files)?

    – Jamie_D
    Nov 15 '18 at 17:03











  • @Jamie_D I think the rule needs to be like: have we directly visited a file grab the part after the word upload and add it to the query string. Once there I can run a function to check user permissions.

    – James Glendenning
    Nov 15 '18 at 17:11











  • Updated answer for accessing user info and file parmeters ...

    – Jamie_D
    Nov 15 '18 at 18:15













0












0








0








When a user directly accesses the uploads directory I need to be able to redirect the user to index.php?file=$1. For example:



someurl.com/uploads/sites/17/2018/10/image.jpg


to



someurl.com/index.php?file=/sites/17/2018/10/image.jpg


From this url I will be able to check against the current logged in users meta.










share|improve this question
















When a user directly accesses the uploads directory I need to be able to redirect the user to index.php?file=$1. For example:



someurl.com/uploads/sites/17/2018/10/image.jpg


to



someurl.com/index.php?file=/sites/17/2018/10/image.jpg


From this url I will be able to check against the current logged in users meta.







wordpress apache .htaccess






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 13:42









Abhishek Gurjar

5,90772433




5,90772433










asked Nov 15 '18 at 11:40









James GlendenningJames Glendenning

7017




7017












  • Do you want to prevent direct access to the entire uploads directory or just images (or other files)?

    – Jamie_D
    Nov 15 '18 at 17:03











  • @Jamie_D I think the rule needs to be like: have we directly visited a file grab the part after the word upload and add it to the query string. Once there I can run a function to check user permissions.

    – James Glendenning
    Nov 15 '18 at 17:11











  • Updated answer for accessing user info and file parmeters ...

    – Jamie_D
    Nov 15 '18 at 18:15

















  • Do you want to prevent direct access to the entire uploads directory or just images (or other files)?

    – Jamie_D
    Nov 15 '18 at 17:03











  • @Jamie_D I think the rule needs to be like: have we directly visited a file grab the part after the word upload and add it to the query string. Once there I can run a function to check user permissions.

    – James Glendenning
    Nov 15 '18 at 17:11











  • Updated answer for accessing user info and file parmeters ...

    – Jamie_D
    Nov 15 '18 at 18:15
















Do you want to prevent direct access to the entire uploads directory or just images (or other files)?

– Jamie_D
Nov 15 '18 at 17:03





Do you want to prevent direct access to the entire uploads directory or just images (or other files)?

– Jamie_D
Nov 15 '18 at 17:03













@Jamie_D I think the rule needs to be like: have we directly visited a file grab the part after the word upload and add it to the query string. Once there I can run a function to check user permissions.

– James Glendenning
Nov 15 '18 at 17:11





@Jamie_D I think the rule needs to be like: have we directly visited a file grab the part after the word upload and add it to the query string. Once there I can run a function to check user permissions.

– James Glendenning
Nov 15 '18 at 17:11













Updated answer for accessing user info and file parmeters ...

– Jamie_D
Nov 15 '18 at 18:15





Updated answer for accessing user info and file parmeters ...

– Jamie_D
Nov 15 '18 at 18:15












1 Answer
1






active

oldest

votes


















0














Edited from this answer :



RewriteCond %REQUEST_URI .(pdf|zip|jpeg|jpg|png)$ [NC]
RewriteCond %REQUEST_FILENAME -s
RewriteRule ^wp-content/uploads/(.*)$ index.php?file=$1 [QSA,L]


Updated for template redirect function:



function check_access_metadata()

$dirCheck = pathinfo($_SERVER['REQUEST_URI']);
$requestFile = $dirCheck['basename'];
if( strpos ( $dirCheck['dirname'] , 'uploads' ) > 0 )
if ( is_user_logged_in() )
//Do something with registered user
else
//Do something with guest



add_action( 'template_redirect', 'check_access_metadata' );





share|improve this answer

























  • Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

    – James Glendenning
    Nov 15 '18 at 18:34











  • I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

    – Jamie_D
    Nov 15 '18 at 18:52











  • If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

    – James Glendenning
    Nov 15 '18 at 18:57











  • Check this solution

    – Jamie_D
    Nov 15 '18 at 19:03











  • Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

    – James Glendenning
    Nov 15 '18 at 19:25










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318702%2fcreate-a-redirect-from-the-attachments-directory-to-index-php-with-the-slug-as-a%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Edited from this answer :



RewriteCond %REQUEST_URI .(pdf|zip|jpeg|jpg|png)$ [NC]
RewriteCond %REQUEST_FILENAME -s
RewriteRule ^wp-content/uploads/(.*)$ index.php?file=$1 [QSA,L]


Updated for template redirect function:



function check_access_metadata()

$dirCheck = pathinfo($_SERVER['REQUEST_URI']);
$requestFile = $dirCheck['basename'];
if( strpos ( $dirCheck['dirname'] , 'uploads' ) > 0 )
if ( is_user_logged_in() )
//Do something with registered user
else
//Do something with guest



add_action( 'template_redirect', 'check_access_metadata' );





share|improve this answer

























  • Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

    – James Glendenning
    Nov 15 '18 at 18:34











  • I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

    – Jamie_D
    Nov 15 '18 at 18:52











  • If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

    – James Glendenning
    Nov 15 '18 at 18:57











  • Check this solution

    – Jamie_D
    Nov 15 '18 at 19:03











  • Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

    – James Glendenning
    Nov 15 '18 at 19:25















0














Edited from this answer :



RewriteCond %REQUEST_URI .(pdf|zip|jpeg|jpg|png)$ [NC]
RewriteCond %REQUEST_FILENAME -s
RewriteRule ^wp-content/uploads/(.*)$ index.php?file=$1 [QSA,L]


Updated for template redirect function:



function check_access_metadata()

$dirCheck = pathinfo($_SERVER['REQUEST_URI']);
$requestFile = $dirCheck['basename'];
if( strpos ( $dirCheck['dirname'] , 'uploads' ) > 0 )
if ( is_user_logged_in() )
//Do something with registered user
else
//Do something with guest



add_action( 'template_redirect', 'check_access_metadata' );





share|improve this answer

























  • Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

    – James Glendenning
    Nov 15 '18 at 18:34











  • I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

    – Jamie_D
    Nov 15 '18 at 18:52











  • If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

    – James Glendenning
    Nov 15 '18 at 18:57











  • Check this solution

    – Jamie_D
    Nov 15 '18 at 19:03











  • Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

    – James Glendenning
    Nov 15 '18 at 19:25













0












0








0







Edited from this answer :



RewriteCond %REQUEST_URI .(pdf|zip|jpeg|jpg|png)$ [NC]
RewriteCond %REQUEST_FILENAME -s
RewriteRule ^wp-content/uploads/(.*)$ index.php?file=$1 [QSA,L]


Updated for template redirect function:



function check_access_metadata()

$dirCheck = pathinfo($_SERVER['REQUEST_URI']);
$requestFile = $dirCheck['basename'];
if( strpos ( $dirCheck['dirname'] , 'uploads' ) > 0 )
if ( is_user_logged_in() )
//Do something with registered user
else
//Do something with guest



add_action( 'template_redirect', 'check_access_metadata' );





share|improve this answer















Edited from this answer :



RewriteCond %REQUEST_URI .(pdf|zip|jpeg|jpg|png)$ [NC]
RewriteCond %REQUEST_FILENAME -s
RewriteRule ^wp-content/uploads/(.*)$ index.php?file=$1 [QSA,L]


Updated for template redirect function:



function check_access_metadata()

$dirCheck = pathinfo($_SERVER['REQUEST_URI']);
$requestFile = $dirCheck['basename'];
if( strpos ( $dirCheck['dirname'] , 'uploads' ) > 0 )
if ( is_user_logged_in() )
//Do something with registered user
else
//Do something with guest



add_action( 'template_redirect', 'check_access_metadata' );






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 18:14

























answered Nov 15 '18 at 17:08









Jamie_DJamie_D

51938




51938












  • Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

    – James Glendenning
    Nov 15 '18 at 18:34











  • I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

    – Jamie_D
    Nov 15 '18 at 18:52











  • If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

    – James Glendenning
    Nov 15 '18 at 18:57











  • Check this solution

    – Jamie_D
    Nov 15 '18 at 19:03











  • Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

    – James Glendenning
    Nov 15 '18 at 19:25

















  • Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

    – James Glendenning
    Nov 15 '18 at 18:34











  • I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

    – Jamie_D
    Nov 15 '18 at 18:52











  • If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

    – James Glendenning
    Nov 15 '18 at 18:57











  • Check this solution

    – Jamie_D
    Nov 15 '18 at 19:03











  • Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

    – James Glendenning
    Nov 15 '18 at 19:25
















Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

– James Glendenning
Nov 15 '18 at 18:34





Works, but is a little too harsh, is there any way to only use this rule if the file is accessed directly / downloaded and not just used in an image tag?

– James Glendenning
Nov 15 '18 at 18:34













I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

– Jamie_D
Nov 15 '18 at 18:52





I'm still a bit hazy on what you want to accomplish. Can you clarify the best possible outcome? Do you actually want to redirect users, or do you want to run a function when they access an object in the uploads directory?

– Jamie_D
Nov 15 '18 at 18:52













If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

– James Glendenning
Nov 15 '18 at 18:57





If someone directly goes to the attachment url / right click downloads then I want to run a function that checks if the user is logged in.If they are then download away, else 404.However I want the images to display on the website, in an image tag for example, even if the user is not logged in.

– James Glendenning
Nov 15 '18 at 18:57













Check this solution

– Jamie_D
Nov 15 '18 at 19:03





Check this solution

– Jamie_D
Nov 15 '18 at 19:03













Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

– James Glendenning
Nov 15 '18 at 19:25





Hmm, this is for hot linking. It doesn't redirect if I view the attachment URL.

– James Glendenning
Nov 15 '18 at 19:25



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53318702%2fcreate-a-redirect-from-the-attachments-directory-to-index-php-with-the-slug-as-a%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線