How to remove "wp-content/uploads/2019/10" from media file URL? - php

For a current project, I am supposed to remove "/uploads/2019/10" from specific media file URLs. For instance, I need this, "https://www.mywebsite.com/wp-content/uploads/2019/10/myAwesomedoc.pdf", to be "https://www.mywebsite.com/myAwesomedoc.pdf". How can I accomplish this?
BACKGROUND: This needs to be done only for PDFs on the site. So far, I've tried redirects, through plugins (Pretty Link) and via .htaccess file per the direction of a peer but this didn't work because redirects don't change the actual "home" URL.
I've read elsewhere that in wp-config.php that I can insert code like define( 'UPLOADS', 'files' ); but this would affect all files, not to mention I would need to relocate all PDFs to this new folder called "files".
Lastly, I'm aware that under Settings>Media , I can disable the uploads folder from sorting in YYYY/MM format.
Here are an example of what I produced in .htaccess for redirects
# New redirects
redirect 307 /wp-content/uploads/2019/10/myAwesomedoc.pdf /myAwesomedoc.pdf
This wasn't fruitful as, unlike redirect plugins, this made the file system look for a file called "myAwesomedoc.pdf" in the root file, which of course didn't exist.
After the "redirect link" method wasn't satisfactory, I attempted to go the "wp-config.php" route and with what I found, I wasn't able to find a way to only change the various paths a PDF file may have on this website [e.g., "https://www.mywebsite.com/wp-content/uploads/2019/10/myAwesomedoc.pdf", "https://www.mywebsite.com/projects/assignments/myAwesomedoc.pdf", "https://www.mywebsite.com/products/documents/myAwesomedoc.pdf"].
I appreciate any help I may get on this.

I guess you got confused between Redirection and Url rewriting!
There is a possible way of doing that with .Htaccess by matching pdf extensions with regex and pointing them to the real path in media directory.
But some anomalies would happen cause of different dates patterns used by wordpress!
I think Redirection wouldn't solve the issue unless files are already moved to root directory.
How about you write your own plugin for this feature?
Would this feature be shared with other users (editors, contributors...)?
For PDF files you don't any media processing functionalities (resizing...)
So The plugin would:
- upload files "Safely" to root folder.
- List only pdf files
It depends on how you are publishing those PDFs.. If they are meant for download only then you just share their links.
Otherwise with a viewer or extra information , you could define a custom post type for those docs and use custom fields...
Then you redirect old files urls to new location.

Related

PHP - How to use the same set of files in each subdirectory?

You see how facebook works, like if my profile is
www.facebook.com/myusername
then there is a specific index.php file and other lots of files that open when I open facebook.com/myusername.
Now I believe it is highly unlikely that Facebook copied the same files into each and every user's username directory.
How else would it work?
I'll be having many different users using the same application, i.e. the same set of files with minor changes in one or two files. Do I necessarily have to copy all the files into all the user directories each time?
I tried putting an index.php file in the subdirectory that contains this code:
<?php
require '../index.php';
?>
Now even though that runs the ../index.php file inside the subdirectory, but when the ../index.php file redirects to say another file named 'otherfile.php', then it gives a 404 not found error, because 'otherfile.php' is not present in the subdirectory, it is present in the parent directory.
How do I solve this problem?

Custom directory viewer (php)

I started a little project a few days ago, a directory viewer. (Not a redesigned htaccess thing.)
It's written in PHP and works great except for a few little things.
I have one file (masterfile) where all parts of the viewer (css, php, ..) come together and build the final viewer. Whenever you access a directory without an index.php, index.html, etc. in it, you should end up in this masterfile and see your directory (-content).
Example: example.com/css/ => You're in css dir => Show custom dir viewer (css folder)
Idea: Disable .htaccess indexing which produces an 403 error, redirect this error to masterfile.
Options -Indexes
ErrorDocument 403 /masterfile.php
This does work, however it lists the content of the masterfile directory and not the content from original folder (example: /css/) Ideas?
Possible solution (I don't like): Put a file, that includes this "masterfile", in EVERY directory and name it index.php
I hope you guys have some ideas, I appreciate any help!
You can put something like this at the top of masterfile.php:
$parsed = parse_url($_SERVER['REQUEST_URI']);
$files = scandir($_SERVER['DOCUMENT_ROOT'].$parsed['path']);
maybe with some adjustment depending of your server configuration. The parse_url stuff is to remove eventual GET and/or segments.

PHP url functions and subdomains

I have a website thenoblesite.com. It has some subdomains i.e.
download.thenoblesite.com
wallpaper.thenoblesite.com
etc.
Pages for subdomains are present in the main htdocs folder i.e.
httpdocs/download <- download.thenoblesite.com
httpdocs/wallpaper <- wallpaper.thenoblesite.com
Problem is that I am using $_SERVER['DOCUMENT_ROOT'] . '/css'; for css folder and other common folders(graphics, includes, script etc). However in the subdomain page download.thenoblesite.com, $_SERVER['DOCUMENT_ROOT'] will refer to download.thenoblesite.com root folder, not the main thenoblesite.com root folder where css,graphics and includes folders are present.
I have to place the same graphics, css and includes folders separately on all subdomains. Every time I update the website I have to copy the common folders to all subdomains folders.
Another related problem is that i have to use absolute linking for the large sized downloads folder for e.g. VLC media player I have to use thenoblesite.com/download/vlc.exe or i also have to duplicate the large size download folder in all subdomain folders. This method unnecessarily increases the website size , creates confusion when I update the site and doesn't look good programming practise. Is there any possible PHP solution so that i can use the same css, images, downloads and includes folder for all subdomains....
I am not sure if this is something you might be interested in, but you could always create a new subdomain and call it something like style.[domain] and create a new variable in your config file and point it to that. this way you have all the images and css files etc stored in one place and if your traffic spikes you can always move that subdomain to a CDN etc so its really customizable.
UPDATE
ok so you can simply use a new variable in your config file like below :
$_config['http'] = 'http://www.yousite.com/';
now you can just use this variable to point to all your downloads etc on the main site rather than each pointing to the subdomain's folder. and if you want to be more flexible you can also add a few more css or js folders like :
$_config['http'] = 'http://www.yousite.com/';
$_config['css'] = $_config['http']."css";
$_config['js'] = $_config['http']."js";
the solution above will also help you if you decided to move the files around or just move a certain folder around etc. this is a good practice if you can adopt it.
You might be able to use an alias in htaccess (or the server config) :
Alias /images /home/username/public_html/images
If that's not possible, you could rewrite all requests to /images via htaccess:
# Untested - should get you on the right track though
RewriteEngine On
RewriteRule ^/images/(.*)$ http://yourdomain.com/images/$1 [R=301,L]

How to correctly link files?

From my previous experience, I've almost always had problems with linking files with my website projects.
For example, linking CSS styles, Javascript files and including files in PHP. The problem is, that on my PC, the directory of my project was /www/project-name/ and when I put the project on a server, the directory would be just /www/. When I uploaded the project to a server, images wouldn't show, styles wouldn't work, database connections wasn't set, functions were not defined etc...
So my question is: What is the best and most efficient way to link/include files?
Something that will work no matter what the directory of the project is, and possibly, if I include project/includes/mysql.class.php in file1.php, and I move that file to a different directory, it would still properly include project/includes/mysql.class.php
You should use relative paths.
Instead of specifying the full path ('/www/project-name/includes/whatever.php'), use a path relative to the current location:
'./includes/whatever.php'
you can define the document root directory of project and then, include all files depending on it
put
define(DOC_ROOT, realpath(direname(__FILE__));
in your front controller, and when you have to include a file
include(DOC_ROOT . "/includes/file.php");
all frameworks uses this method
I'd suggest using a relative path (eg ../style.css or ../../style.css)
The ../ references the parent directory to the current file.
This is what I do, in general.
I use root relative urls inside html (e.g. src="/images/logo.jpg"). This way I can just copy the html from one page and past it in another without having to worry about the link not working becase the other page is inside a folder.
I relative urls in css, because all the resources I use inside the css, like images, I keep in the same folder as the css file (or a sub-directory of it). I mostly do this because it is shorter (url(img/background.jpg); vs. url(/css/img/background.jpg);). Minor added bonus is you could just copy the css folder to create a new theme based on the old one, without having to change all the urls in the css.
In PHP I use include($_SERVER['DOCUMENT_ROOT'] . '/includes/mysql.php');. You can just copy past the code into another file in another folder and it will still work.
The only time I rarely need to hardcode paths is inside htaccess.

using htaccess to hide database passwords

I have a php class that connects to a database which has the password to the database hard coded into it. I do NOT have have access to folders outside the webroot. Reading this forum and others it seemed that creating a htaccess file with
order allow,deny
deny from all
in the directory with my php classes would do the trick. however after doing some quick testing it seems this also blocks the public files which need access to the database to generate the site. to be clear this is the structure i want:
index.php (public file which calls on php classes that access the database)
php_classes/DatabaseConnect.php (contains the password to the database. i want to hide this from everything that is not uploaded onto mysite --- or better yet only to specific files i name)
...
thanks,
brook
Do not place your PHP code in the webroot. Frameworks will typically use this technique where they only put a bootstrap file in the webroot...you can do that same and place your PHP file with sensitve information above your web root so it cannot be browsed.
Your bootstrap file would #require_once '../safe_dir_above_webroot'.
If you're worried about others seeing the login details to your database, rest assure that it cannot be seen if inserted between PHP tags.
.htaccess is a little tricky with some servers. It seems quite a few setups hate overruling which I can understand.
Since you have suggested that you cannot access folders outside of the root directory, you may just want to do something like this.
define("include_allowed", true);
Call that in the leading file, for instance index.php. When a file is included it should check to see if include_allowed has been set true.
if (include_allowed != true) header('HTTP/1.1 404 Not Found');
This checks to see if it has been included by index.php or which ever file that has defined include_allowed true.
If it fails to return true, a 404 error is sent saying not found to trick users! :)
Since your file is PHP , it will processed by the PHP exe, before being rendered to the client. So the password should not be visible. Having said that to use htaccess to stop view a particular file you can do this
<Files php_classes/DatabaseConnect.php>
Deny From All
</Files>

Categories