I have an app where every request gets sent through the index.php front controller. Pretty standard.
The admin interface will be loaded through domain.com/admin.
We want to put public assets for the admin area in a folder named admin.
The problem is when I add the folder, accessing the /admin URL results in it trying to load domain.com/admin/index.php
Is it possible to ignore the subfolder's index.php, or only allow js/css/image files to be served through the subfolder?
Or we could even put an index.php in there which somehow redirects to the root URL? I tried this but it resulted in an infinite redirect loop.
Yes, you may redefine DirectoryIndex to "forget" about index.php:
DirectoryIndex index.html
So now only index.html will be served if found, otherwise it will be HTTP 404
We ended up just using a different folder name to serve assets.
Related
I have the following folder structure:
+users
-adduser.php
-viewuser.php
When a visitor navigates to example.com/users it's showing the folder structure. I need to restrict visitors ability to see the file listing, either by hiding it or removing it. How can I do that in php?
If you don't want to (or can't) deal with Apache configuration, create an empty file named index.html (or index.php if you prefer) in your users folder.
You can create a .htaccess file on root folder with following;
RewriteEngine on
Redirect 301 /users http://www.example.com/404.html
Then you just need to create 404.html in your website and change example.com with your domain.
Now your /users path will redirect to 404.html.
You can add the following to your Apache virtual host file:
<Directory "path_to_folder">
Options -Indexes
</Directory>
The above rule will disallow directory listing. It was suggested in this post: Using .htaccess, prevent users from accessing resource directories, and yet allow the sourcecode access resources
I have a web application. It's back-end is in PHP. There are some directories inside the application. Some of the directory has index.php file. The Problem is when I am trying to route users to other .html pages in the same directory it's working fine. Now one of the directory has index.php file and in the link I have given the relative path to the folder (So by default it is loading index.php file in that directory from Desktop,Labtop etc) but it is not loading that page when I try to run the same from any mobile browser. After Inspection I realized that I can see somefolder/index.php in the address bar while in mobile it is just till the somefolder i.e it is not trying to load index.php file due to which I get "Not Found" 404 error
You have to set your DirectoryIndex correctly. When calling the directory from an web accessible URL without any file name, it tries to access your DirectoryIndex file, per default it is called index.php, index.html, index.htm, index.cgi etc.. This depends also on your platform. Different systems have different default DirectoryIndex configurations. If those index file does not exist, you should see the directory listing in the web browser, even if the web server user has the rights to list the content of the current directory (special bit under Linux/UNIX and maybe protected by advanced kernel security applications like selinux or AppArmor).
What happens, if you try to access the folder directly without any file name from an desktop browser? Will you get there the HTML output of index.php?
I have the default myIndex.php in a subfolder of public_html.
I added a file .htaccess in public_html.
I added DirectoryIndex /subfolder/myIndex.php in it.
myIndex.php contains links and includes.
Includes seems to work but links dont: no images and no css but menu.php is included. Links in menu.php dont work either.
If I
change .htaccess to DirectoryIndex index.php,
create a file index.php in public_html,
and put a link to "./subfolder/myIndex.php" in it
everything works fine.
I dont understand what happens here.
DirectoryIndex determines what to do, when a client requests a directory. If you have
DirectoryIndex /subfolder/myIndex.php
then exactly this script will be executed every time the client requests a directory, no matter where this directory is, e.g. /anotherfolder/ or /static/.
But when you have
DirectoryIndex index.php
instead, Apache looks for an index.php in the requested directory, e.g. /anotherfolder/index.php or /static/index.php.
I have a wordpress site http://hamroletang.com and it contain a file http://hamroletang.com/sardanjali.html
Now I want sardanjali.html to be my website landing page.
Then it will redirect to index.php which i have done already
The easiest way would be to rename sardanjali.html to index.html and add:
<meta http-equiv="refresh" content="5;URL='http://hamroletang.com/index.php'">
Then, make sure that in your Apache config .html takes precedence over .php
You can rename sardanjali.html to index.html and then change in the apache config the directory index to have index.html before index.php:
DirectoryIndex index.html index.php
On my system this was already the default (ubuntu).
In your redirect you should explicitly redirect to index.php, and not to the domain in general.
If you cannot change apache config files and renaming to index.html doesn't help, then you can move your blog to a subdomain or subdirectory and redirect into that. But that would break all the links to your blog. Probably not what you want.
You can change the default page of your website to whatever you want it to be. In your case specify the single file sardanjali.html as your default page. The settings vary between web servers. e.g. on Apache here are the instructions - http://www.cyberciti.biz/faq/apache-display-or-change-a-default-page-other-than-indexhtml/
And yes, you have to use the refresh in your sardanjali.html to automatically redirect the user to your index.php after the specified time.
Right now I have a assortment of 9 files (javascript, image, stylesheet, php) in the root directory of my web server. I would like too put all of these files in a directory called home (for home page). So now at this point I can view my home page at http://example.com/home.
I would like it so that when you visit http://example.com/ it points to the files in the home directory.
I am using php so my first attempt was to create a index.php in the root and include the index from home. This breaks relative URLS within styles, javascript, and includes. To combat this I prepended my includes with $_SERVER['DOCUMENT_ROOT'].'/home/. More problems arise when I try to redirect http://example.com/home to http://example.com/.
Is their any better way of doing this, possibly with .htaccess?
Yes, if your server supports mod_rewrite, you could probably add a RewriteRule:
RewriteRule /(.*) /home/$1 [R,L]
In your Apache httpd.conf change the DocumentRoot to your "home" folder.