Why can some urls contain "fake" directories? - php

Currently, I'm trying to set up updates for passes that are added to the Wallet app on iOS.
One thing that is interesting is that having the url https://example.com/index.php/var1/var2 still works and index.php is still run. Is there a reason why this url format works?

.htaccess/mod_rewrite is the reason why it's working.
For example:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?path=$1 [NC,L,QSA]
Having this rules inside a file named .htaccess in the root folder of your website, will make the path /var1/var2 available inside $_GET['path']

Related

Why the url in which index.php is prefix with first segment in codeigniter works | Codeigniter | RewriteCond

I am working on a CodeIgniter project and today I found a very strange issue.
When I open the URL that is prefixed with index.php in the first segment it is still working even though I expect the URL to return a 404 Not Found page.
For example, the URL of my website is http://localhost/project and when I open the URL http://localhost/project/jobs it works fine, but when I open http://localhost/project/index.phpjobs it also works.
I don't know what is going on over here!
Please note that the URL doesn't include slash but is still working and that is not a typo.
Please check in your project and let me know if someone have the same problem because I think this problem may also exist in your current project but not noticed.
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin(.*)$ admin/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^.well-known/ - [L,NC]
Your first rewrite rule
RewriteRule ^admin(.*)$ admin/index.php?/$1 [L]
will be honored only if the previous two conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
have been met.
Now, the trick lies in these conditions which basically say that the rewrite will be performed only if the requested resource (REQUEST_FILENAME here) does not exist as either a file or a folder.
Since index.php obviously exists the rewrite rule is skipped and the server actually receives the original (non-rewritten) request.
That is the reason why you see the same result for requests that both do and do not contain /index.php/ as prefix.
The same applies for both sets of rewrite, the one you are using for your admin page and the regular one.

Using HTaccess to redirect to a subfolder if it contains the requested file

I'm trying to load some content from the database, and cache it in a folder on the server. Currently, I'm using this code to catch requests to http://localhost/files/hash.png and pass the hash and filetype to a PHP file which returns the data.
Files can have any extension, so I need to either catch the full filename (that's fine), or split it into name and extension (also fine).
Here's my HTaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^files/([^\.]+)\.([^\.]+)$ getfile.php?hash=$1&ext=$2 [NC,L]
The problem is, I'm now needing to tell it to try to find hash.png in a subfolder of files called cache (i.e. files/cache/hash.png). This would need to be loaded instead of the PHP file, should the file exist in that folder.
Can anyone help? I'm struggling to get it working. This is to do a kind of caching on files stored in a database.
RewriteEngine On
RewriteRule ^files/(.*) files/cache/$1 [NC]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule *. - [L]
RewriteRule ^files/cache/(.*) files/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^files/([^\.]+)\.([^\.]+)$ getfile.php?hash=$1&ext=$2 [NC,L]
Here it's rewriting the file name to alter the path to file/cache/ before testing if it exists. If it exists, don't change the file name and stop rules (with [L]). If it doesn't exist, rewrite the file name back to what it was and continue with the rules you already have.

Flight PHP Routing from Subdirectory

So I'm using the Flight PHP microframework (http://flightphp.com/) to do routing. My question is, how can I run the router from within a subdirectory? What I mean is, essentially, run it 'sandboxed' within a folder.
As in, a request to '/' just pulls the regular index.php file. But a request to '/flight/file' would load the URL using Flight.
I know you can't just dump it in a folder on the server and expect it to work because FlightPHP expects the URLs relative to the root.
Is there a way to run FlightPHP isolated in a directory with the rest of the website running regular PHP?
EDIT
I tried simply putting the .htaccess file into the subdirectory. This has the peculiar effect of causing the routes to still act as if they are from the root (e.g. /thing/otherthing/ when it should be /otherdirectory/thing/otherthing/ ) while simultaneously causing the 404 callback to not work. Not what I intended.
EDIT 2
Contents of .htaccess file, which are what is suggested by the Flightphp website:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
I know this is an old question but I've been doing something similar just by adding something along the lines of
RewriteBase /flight/
in the .htaccess file (before all of your rules) of the flight directory. Hope this helps someone else looking for the same thing. Example:
RewriteEngine On
RewriteBase /flight/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Sorry for giving you an answer that is not going to help a lot. I'm running Flight (also sandboxing) on a subdirectory. I've created an .htaccess just with the defaults in the subdir and Flight is now regarding this as it's root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Did you check the content of the .htaccess on the higher levels, maybe there is something that is blocking you

.htaccess not working for php/mvc in subdirectory

Desired goal:
On an existing website, I need to implement a new website. The new site needs to be 'staged' implementation, so a subdirectory for 'dev' and for 'test' and for 'site' is made. All three subdirectories contain an identical php/mvc implementation except for the fact that different useraccounts and databases are used. The current site which resides in the rootdirectory should be kept working as well.
Current configuration attempt:
I have put an .htaccess file in the root directory of the current website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/dev(.*)$ <url>/dev/$1
RewriteRule ^/test(.*)$ <url>/test/$1
RewriteRule ^/site(.*)$ <url>/site/$1
Note: instead of there is a complete url.
This should redirect all urls with 'dev' in it, to /dev/$1 and $1 contains all the rest of the parameters.
Result:
When the homepage of 'dev' of 'test' or 'site' is called. The homepage shows up perfectly fine.
Problem:
Everytime I click on a menu-item or a link, the resulting url gets rewritten to the right url, which is: /dev/volunteering?page=volunteering&lang=nl
but I get an errormessage 404:
The requested URL /dev/volunteering was not found on this server.
Please keep in mind that it is about a custom mvc implementation.
Required Help:
What change is necessary to make the browser understand that this kind of requests should not be handled by the current website but that, depending of 'dev' or 'test' or 'site', the request should be forwarded to the appropriate subdirectory in such way that the mvc implementation understands?
Please try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(dev)(volunteering([^?]*)) $1/$2?page=$2&lang=nl [L,NC]
RewriteRule ^(test)(volunteering([^?]*)) $1/$2?page=$2&lang=nl [L,NC]
RewriteRule ^(site)(volunteering([^?]*)) $1/$2?page=$2&lang=nl [L,NC]

Create dynamic subdirectories using htaccess and php

Every single time a user registers on my site I would like them to have their own subdirectory with their registered "username". Every user subdirectory will have the same "index.php" file which will do something.
For example: "/users/username1/" and "/users/username2/"
If some one wants to access the subdirectory they would simple go to:
"www.example.com/users/username1/" or "www.example.com/users/username2/"
The easy and messy solution would be to simply create a subdirectory for every user and place the same "index.php" file in every directory. But to me this is only going to crowd my server space and make my directories large.
I wanted to know if all this can be done using .htaccess? Can I create one "index.php" and one ".htaccess" file and place them both in my "/users/" directory? What would be the actual code that I would have to place in my .htaccess file??
If you have a better way of doing this please let me know. I am using Apache and PHP as my working environment.
Thank you
Well, for example, you could do it all with one htaccess like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
What it does:
switches on rewrite engine
checks if a requested file exists
checks if a requested directory exists
if NOT, it redirects request to your main index.php
Basically that means if you enter url such as yourdomain.com/users/ivan/, you request will be redirected to:
index.php?url=/users/ivan
then you $_GET['url'] in your index.php and split it into pieces.
That's just an example, there other mod_rewrite methods to do this.
Make it virtual. There are no subdirectories, you can use mod_rewrite to simulate that.
With mod_rewrite you can make /users/username1 lead to /users.php?user=username1 for instance. Everything is transparent for the client, he wont notice what is really happening.
By using something like this:
RewriteEngine On
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
You can customize RewriteRule as much as you want.
You can essentially type in any directory you want, and it will be redirected to your index.php page.
If you want to make sure the existing directories are not redirected, do this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
If you want to limit the scope, so only a subdirectory of user/ is redirected (similar to Stack Overflow), simply add in 'user' to the start of the rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ index.php?a=$1 [L]
And finally, if you want to have an individual file handle all user requests seperate from your actual index.php page:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/([\-_0-9A-Za-z]+)$ users.php?a=$1 [L]
This is a very similar setup I use to distribute CSS files.
Note: The Directory will be contained is $_GET['a']

Categories