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
Related
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]
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.
I'm using Codeigniter, and I've separated out the resources (CSS, JS, Images, etc.) out of the Application folder, into a folder like so: root/resources. CodeIgniter has the typical htaccess file in the root folder, and the contents are:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /index.php?/$1 [L]
If I wanted to allow resource files to live inside a sub-folder, inside the application folder, like so: /application/plugins/some_plugin_name/resources. What contents would I put inside the htaccess file inside the above resources folder that would allow me to access CSS, JS, and other resources files like images, etc.?
This is in regards to future plugins as well, so the "some_plugin_name" folder can be anything.
You just need this:
RewriteEngine On
By turning on the rewrite engine inside a subfolder, its rules (which are none) will take precedence over any rules in the parent directory.
Jon Lin was close, but there is an additional rule to add onto it. Here's the complete htaccess file that works inside the resources folder. You may want to add it to any folders below the resources folder (but I may be wrong there):
RewriteEngine On
allow from all
The reason I posted my own answer is because it truthfully took me like 30 minutes to find the answer to it.
Hi, so this may be asked elsewhere but I have searched and come up with irrelevant results.
I clearly don't know what to search for exactly.
I'm just trying to rewrite everything after a certain directory to that directories index.php.
Here is an example of the URL a visitor would SEE
website.com/search/location/United%20States
And I would like that URL to be rewritten server-side so that it loads website.com/search/location/index.php
(not a 301 redirect)
I would like the Url to stay the same but load the index.php script (to include United%20States so this can be passed to PHP to determine what the location is and if it is legitimate etc.).
Sorry I know that this will be somewhere already but I just can't find it
I have some code already but it is buggy and seems to choose when it wants to work and also sometimes uses location/index.php/United%20States which is not what I want.
Put this code in your htaccess (which has to be in your root folder)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/location/.+$ /search/location/index.php [L]
If you have Apache web server, make sure you have mod-rewrite enabled and put .htaccess file into your WEBROOT/search/location directory. Put this into .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php [L]
This will internally redirect all requests, where file or directory does not exists, to index.php.
You could also put .htaccess file into your WEBROOT directory and write this into to:
RewriteRule ^search/location/.* /search/location/index.php
Hope this helps.
I'm developing a website which has three sub folders in the main directory as /a/, /b/ and /c/. Contents in main directory like site.com, site.com/a/, site.com/b/ and site.com/c/ are different; however, the codes and files are completely similar. In order to reduce the volume of the codes, I want to find a way to delete all code files in my sub folders and so all requests to be responded by the main directory files while I keep the sub folders. Could you please give me your opinion about changing the index.php, .htaccess or etc to solve this problem?
You can do this, for example:
RewriteEngine On
RewriteRule ^(a|b|c)/(.*) $2?folder=$1 [L,QSA]
This will make all requests to a/smth, b/smth, c/smth be rewritten to smth (in the root directory) and a/b/c passed as query-string parameter 'folder'.
However, when you access static files like this, a/image.png, b/image.png (for instance) are still considered different uris - and as such will be downloaded separately by the browsers (instead of caching). So you should consider treating resources in a different way. for example, make a separate folder for static resources and address it directly from each subfolder.
For more information, read mod_rewrite manual
Make sure sure there is not .htaccess in /a/ OR /b/ OR /c/ directories
Place this rule in root .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[abc]/(.+)$ /$1 [L,NC]