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.
Related
I have requests coming to my server already. I want to v2 out my API calls.
Currently I have the directory /v2/ and within that I have index.php
If I call /v2/index.php?path=users/123 I get the right data back from my database. All is well.
But to standardize my code, I want to redirect /v2/users/123 to /v2/index.php?path=users/123
I have mod_rewrite loaded in Apache, tested with phpinfo(). I am not sure whether I need to place my .htaccess within the v2 directory or at root. I do not have any .htaccess in root today, and I do not want to modify any calls not made to /v2/.
Any pointers where to place the .htaccess file and the right conditions to do so?
My current file is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /v2/index.php?path=$1 [NC,L,QSA]
I have put it in the /v2/.htaccess directory, but navigating ahead of /v2 yields 404 errors.
Thanks!
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
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 have a directory site and its sub-folder and files in it.
It also contain a .htacess and below is the code
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?cc=$1
so my below url will is effective and working good.
http://localhost/site/sweetinc
And below code in index.php below
<?php
if (isset($_GET['cc']) and !empty($_GET['cc'])) {
echo $_GET['cc'];
} else {
die("Sorry wrong code");
}
?>
And this is working good.
Now, i want to access or display all files as normally
http://localhost/site/sweetinc/home.php
http://localhost/site/sweetinc/test.php
home.php and test.php are located in site directory. And if it redirects to other sub-folders then it should
be visible like
http://localhost/site/sweetinc/sub-folder1/test3.php
Is this possible, as I am working seperating a group using seperate directory using .htaccess and considering all files in site directory as base files
The trick here (if I understand your question correctly) is that you don't want the rewrite rule to be in effect when you're trying to reach a file that actually exists, right?
In that case, just before your RewriteRule, add these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That way, the rewriterule only takes effect when the url (/site/sweetinc for example) does not really exist on the server. If it does (/site/sweetinc/home.php) the rewriterule is skipped and the file is shown.
(-f checks if the filename exists, -d checks if the directory exists, so /home/sweetinc/somedir/ should work too)
Update based on updated question
You need two separate rules for this. First of all, if the /sweetinc/ directory in the url is used, refer them to the /site/ folder:
RewriteRule ^sweetinc/(.*)$ /$1 [L]
Then, if the file does not actually exist, let the index.php handle it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cc=$1 [L]
Some examples:
I have the following files in my files subdomain:
/index.php (which has the same content as yours does, so it reads out ?cc=
/circle3.PNG
/the .htaccess with the above rules
http://files.litso.com/sweetinc/hello shows the index.php that echoes "hello"
http://files.litso.com/sweetinc/circle3.PNG shows the actual file in /
(note, I don't have a /sweetinc/ directory, it's all faked)
It's quite common for us to put in progress sites at www.domain.com/dev/ and then once the client has signed off the site to move it to the top level www.domain.com, what we like is to be able to put a .htaccess file in the top level so that once we've moved the site out of /dev if the client accidently goes to www.domain.com/dev/apage.php that they be redirected to www.domain.com/apage.php, but only if www.domain.com/dev/apage.php doesn't exist.
Sometimes the dev folder will be called various other things, and ideally we don't want to have to edit the .htaccess file to match the folder name.
Thanks for any help.
You could do something like
Edited based on comments:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule dev/(.*)$ $1 [R=301,L]
Which basically means that if the file doesn't exist - RewriteCond %{REQUEST_FILENAME} !-f - then rewrite any request to dev back to the root. You have to specify dev/ in the rewrite rule as otherwise you will get stuck in a redirect loop.
This will only work however if you are using explicit files rather than a framework with everything routed through index.php for example