Selective directory index from htacess
Apache Module mod_dir provides "trailing slash" redirects and serving directory index files. "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."
How to have different index to sever from the same directory?
Say for example, for the same root directory one needs to server index.html as home page and index.php for other pages.
How can such a thing be achieved with htaccess?
This is done by a simple RewriteCond Directive
- Description: Defines a condition under which rewriting will take place
- Syntax: RewriteCond TestString CondPattern
- Context: server config, virtual host, directory, .htaccess
- Override: FileInfo
- Status: Extension
- Module: mod_rewrite
- The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive.
As per the example, we need homepage to be index.html and rest to be severed by index.php, so in the same directory there are two index files index.html and index.php, but Apache picks the one it finds first, so we need to do the below :
Set the default handler to DirectoryIndex index.html index.php
Write a rule as below :
<IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{REQUEST_URI} !^/(index.html)?$ RewriteRule ^(.*)$ index.php?q=$1 [L,QSA] </IfModule mod_rewrite.c>
That is : Rewrite URLs of the form 'x' to the form 'index.php?q=x' for all but index.html. There by achieving the need! There are always better ways of doing things, share your ways below!
Recent blog posts
- watir-webdriver web inspector
- gem list to gemfile
- Packing ruby2.0 on debian.
- Made it into The Guinness Book!
- to_h in ruby 2.0
- Filter elements by pattern jQuery.
- Better HTML password fields for mobile ?
- Grayscale image when user offline
- nth-child CSS pseudo-class Christmas colors
- EventEmitter in nodejs