I have a directory of files which are downloaded by users package managers using the direct link to the file.
I'm trying to set up file logging, so I can get statistics on the downloads. I’m using this script I found on GitHub: https://github.com/iNamik/PHP-Download-Tracker
I'm using the above script that consists of a files directory, a log directory, and an index.php which lists the files to download.
Index.php lists all files in the download directory. This file can be renamed to anything, i.e. download.php
If I use the index.php and click on the file I want to download, it logs the information and downloads it.
If I use the direct link to the file (/downloadfolder/file.exe) the index.php is bypassed and nothing is logged.
Is it possible to use something like mod_rewrite in Apache, to add download.php?file= before the file name in the direct link?
Example:
Access this Direct link: https://exampledomain.com/files/file.pdf --> this does not get logged. File transfer starts.
Have it be automatically rewritten to: https://exampldomain.com/files/download.php?file=file.pdf --> This does get logged. And the file transfer starts
After some hair pulling and testing using a htaccess tester, this is what I came up with.
My download.php is inside the files directory.
RewriteEngine On
RewriteBase /files/
RewriteRule ^(.+(file|FILE))$ download.php?file=$1 [L]
This will redirect anything that doesn't exist to index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]
Related
I am trying to improve the structure of my urls by using .htaccess. I have a file named foo.php and a folder named /foo. I want to be able to access example.com/foo and show example.com/foo.php. I also want to be able to access example.com/foo/bar and show example.com/foo/bar.php. Lastly I want to reditrect from example.com/foo/ to example.com/foo. Does anyone know how to do this?
This is my code:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
I am able to remove the .php extension by I am not able to redirect from the folder to the file. I also tried the solutions below but none of them resulted in the desired behaviour.
htaccess - Rewrite files that have a directory with the same name
.htaccess, rewriting of filename with same name as directory
.htaccess, proper rewriting of directory and file with same name
I found a workaround solution to this problem. In order to remove the extension from file names, I used the following code in my .htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The above code allows me to go to example.com/foo and show example.com/foo.php. In order to redirect from example.com/foo/ to example.com/foo, I created an index.php file in the /foo directory. I added the following PHP code to the file
<?php
$page = str_replace('/index.php', '', $_SERVER['PHP_SELF']);
header("Location: $page");
?>
This redirects from the directory to the file. Hope this helps.
I am working on redesigning a website that is currently a PHP website. The new site will be all HTML. I am trying to keep all of the slugs the same. Should I set up redirects?
Currently, the website pages have the .PHP extension in the browser.
Example: https://www.dehartsystems.com/residential.php
The new page will be HTML and the URL will have no file extension.
Example: https://www.dehartsystems.com/residential
Do I need to set up redirects as the file extension will be changing?
To answer your question, yes you do need to set up redirects. Typically this is done in the .htaccess file in the root of your html folder on your server.
In this .htaccess file you put:
# Check rewriting is possible.
<IfModule mod_rewrite.c>
# Turn on Rewrite engine
RewriteEngine on
# check if file does not exist.
RewriteCond %{REQUEST_FILENAME} !-f
# check if folder does not exist.
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite the given URL.
RewriteRule ^(.*).php$ $1.html [R=301,L]
</IfModule>
This will redirect any call to any .php file that does not exist to the HTML file with the same file name, in the same folder location.
Reference:
https://htaccessbook.com/add-remove-change-file-extensions-htaccess/
See also How to change the PHP file extension using .htaccess file on GoDaddy Linux Hosting?
Yes, you will need to rename every file to .HTML if you don't do so the browser won't recognize the file as code. " https://www.dehartsystems.com/residential.html " only then your website will work., i.e you should rename residential.php to residential.html
I didn't write this web code. I just have to deploy it....
The $WEBROOT directory includes index.php and login.php, among others.
The automatic redirect URL from index.php (which successfully executes when just the domain URL is requested) is http://$HOST.$DOMAIN/login?p=$VALUE
This returns the code 404.
If I manually change the URL to http://$HOST.$DOMAIN/login.php?p=$VALUE
the login page successfully appears.
My first problem is I don't know what keywords to search for in the Apache documentation for this. This question seems close. But, my actual files have the .php extension. My problem is that I have to assume all of the URLs will just request file and not file.php.
How do I tell Apache to look for file.php before it returns a 404 for not finding file?
Create a file named .htaccess with the following content inside the folder your .php files are in:
#turn on url rewriting
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I have written a php script which lists all files and directories from the current directory where the php file is located. I want to use this script for many different directories and it would be great if I can reuse it for all of them. Is this possible without to put a new php file in all these folders? I think of something like htaccess redirect so if the user visits an URL to a specific folder the script is executed with the folder as parameter but it does not lie in the directory itself.
I hope you understand what I want and have any ideas for this?
Yes you need to make an .htaccess file.
So basically your code will reside in index.php which is in the root folder.
Now use the below code in .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
now when the user tries to access sub folders, the folder path will come as a parameter named "path" to index.php
Im trying to have a htacess file within my root folder so i can redirect the user to index.php file, if for example trying to access any file inside my root directory. I failed to do that. I have that file inside my root folder as i should, but when im trying to access files typing the names in the URL, i actually have access to the files. So the .htaccess file does not work.
I have to mention that im trying to do that locally, having wamp installed and using slim framework. I do not know if something mess with these.
The code that i have in my .htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
I have that file inside my root folder as i should, but when im trying to access files typing the names in the URL, i actually have access to the files
That means you want even the existing files not to be shown, and all requests should be redirected to index.php.
If that is the case then why do you have these conditions? Remove these
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Those 2 conditions mean apply that redirect only if the requested url is niether a file nor a directory. No wonder for files that are actually present your redirect is not working. You told it so.