My friend recently told me that I was able to do something like
http://example.com/about/
without having to create a directory and place a index.html inside of it, like this.
http://example.com/about/index.html
How in the world do you do this? I didn't know it was even possible to do unless you created a directory and placed a index.html inside of it, anyway thanks. :)
If you use Apache as WEB server, use .htaccess for you site root directory.
Write following rule here:
DirectoryIndex index.html
Also you can use more than one file here:
DirectoryIndex index.php index.html
In this case first found will be used.
If you don't want to use .htaccess, you can setup same in Apache config file (httpd.conf):
< Directory /foo>
DirectoryIndex index.html
DirectoryIndex index.php
< /Directory>
Here /foo - root directory of your site
Servers like Nginx can allow you to rewrite URLs, so you can place your file wherever you want.
If you use Apache web server then create a file named .htaccess
and write code like below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?load=$1 [PT,L]
If this code when you type a url like www.example.com/about its doing to open your aboutController
Write aabout in this url http://salopek.eu/content/28/create-a-simple-php-mvc-framework
If you does not use Apache or you want simple solution then then you can just create a folder named about and create a simple index.html or index.php file. Then when you type www.example.com/about
it opens your about\index.html or index.php file
I hope it helps
Related
Right now the index.html of my website is loading from the root directory when someone enters www.mydomain.com in their browser, which as I understand it is the default behavior. I want to move all of my website files into a subdirectory (e.g. public) so that when someone loads www.mydomain.com, the index.html file it loads is in the /public/ directory on my server. However, I do not want the URL to change to www.mydomain.com/public. From there, if a link is clicked/, all of the files will be in the public directory. How would I go about achieving something like that
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^((?!public/).*) public/$1 [L,NC]
I want to change the root of my website with php functions.
This is my website (mysite.com)
This website using (current root document) and loading from index.php like this
mysite.com /
index.php
folder1
folder2
now I want when we open (mysite.com) show index.php inside of folder1.(change the root to /folder1/ )
I can use redirect but my address will be like this mysite.com/folder1
but I want this website (mysite.com) using /folder1/ as root directory with out we want to user (mysite.com/folder1)
What should I do? What is the functions for this to use in index.php (to use folder1 as root directory)?
This is an excellent fit for .htaccess usage if you can't access the httpd.conf.
RewriteEngine on
# Change yourdomain.com to be your primary domain.
RewriteCond %{HTTP_HOST} ^(www.)?yourprimarydomain.com$
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteCond %{REQUEST_URI} !^/subfolder/
# Don't change this line.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subfolder' to be the folder you will use for your primary domain.
RewriteRule ^(.*)$ /subfolder/$1
# Change yourdomain.com to be your primary domain again.
# Change 'subfolder' to be the folder you will use for your primary domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?yourmaindomain.com$
RewriteRule ^(/)?$ subfolder/index.php [L]
You can use your .htaccess to change the file it default loads:
# Set the startfile to something else:
DirectoryIndex folder1/index.php
Changing basic functionality is a stupid thing to do*. It's confusing, especially for people who are new to the code, or if you look back in a year or so. It will slow down progress and development in the long run. A big no go!*
*Unless you know what you are doing. Which you're not if you have to ask :)
If you are very sure you want to go through with this, you can set the DOCUMENT_ROOT to something else.
// Place this as high as possible in your code, like a config
$_SERVER['DOCUMENT_ROOT'].= '/folder1';
To provide a more professional method, you could change the document_root in the httpd.conf, and have the php $_server value set to the new value serverwide.
Some other Q&As from SO that might help:
How do I change the root directory of an apache server?
how to remove folder name from url using htaccess
If you really want use php, you can place index.php file in you root dir with this content:
<?php
header('Location: /folder1/');
?>
Althought, better way to do this, is with htaccess.
I need to load single index.html file before actual index.php I have on server.
I have:
index.html - (intro page)
index.php - (actual webpage index file)
.htaccess rewrite rule
Sounds simple, but .htaccess redirects every webpage query back to my index.html file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.html [QSA,L]
</IfModule>
What is the best way to make it work?
Don't use mod_rewrite, use DirectoryIndex and give priority to index.html
DirectoryIndex index.html index.php
Note: While this should work, I don't recommend it as it creates ambiguity. One of those configuration changes that will haunt you later.
You can simply use DirectoryIndex directive for that:
DirectoryIndex index.html index.php
From the documentation:
The DirectoryIndex directive sets the list of resources to look for, when the client requests an index of the directory by specifying a / at the end of the directory name. Local-url is the (%-encoded) URL of a document on the server relative to the requested directory; it is usually the name of a file in the directory. Several URLs may be given, in which case the server will return the first one that it finds. If none of the resources exist and the Indexes option is set, the server will generate its own listing of the directory.
I have a website, say http://mysite.com. I would like to put index.php in a subdirectory, public_html/mysubdir/index.php. I would like public_html/mysubdir/index.php to get executed when the user goes to http://mysite.com. And I would like the url to continue to read http://mysite.com. Is this possible?
If your webserver is Apache you could use URL rewriting with mod_rewrite.
Another option is to create an index.php in the root directory and include index.php in the sub directory.
Rewrite rules may be overkill for this depending on what you want. For just your main index page, this will work...
Simply adding this one line to your .htaccess file:
DirectoryIndex mysubdir/index.php
It will display the page located at mysubdir/index.php while simply showing http://mysite.com in the URL.
I use this method myself. While all of my pages are located in the same subdirectory, the home page is displayed with my domain name by itself (http://www.mysite.com). All other pages show the full URL.
If you also have index pages within deeper subdirectories and want those to come up by default within the subdirectory.
Example:
If you want this page: http://mysite.com/mysubdir/anothersub/index.php
to come up with this URL: http://mysite.com/mysubdir/anothersub/
Then modify the line with another index.php like this...
DirectoryIndex mysubdir/index.php index.php
What this does is tell the server to look for files with those names in that same order. If it can't find the first, it tries the second, and so on.
When you're inside your root at / it finds and then displays mysubdir/index.php.
When you're inside another subdirectory like /mysubdir/anothersub/, it can't find anything named mysubdir/index.php so it goes to the next item and displays index.php
You could use a .htaccess file and define Rewrite rules.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Make sure that mod_rewrite is enabled and then place .htaccess file in your root directory with something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ your_subdir/index.php/$1 [L]
</IfModule>
The file structure looks like this
root
root/x
root/x/y.php
root/index.php
.htaccess code
RewriteRule (.*) /x/$1 [L]
What the htaccess file does is to remove the folder name (x) from the url so accessing y.php = http://localhost/y.php instead of http://localhost/x/y.php . This works but my problem now is the index.php shows something like this:
Index of /x
Parent Directory
y.php
I can't access the index.php. I believe the x became the root folder.
Thanks for your help!
First, you shouldn't allow index displays like this on a public server ... on your localhost it may be okay, but still, you can disable it in your .htaccess by adding this:
Options -Indexes
To address your question, you should probably add the following conditions before your rewrite rule so that the rule won't apply to any files or directories that actually exist on disk:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This won't fix the issue you have because the .htaccess is redirecting to a non-existant /x/index.php file ... this is why it's showing the index listing for the /x directory