.htaccess Mod Rewrite

Posted on 2007-10-14
Last Modified: 2008-02-07
I am running into a problem with mod-rewrite, my mod rewrite will not include pages that are index.html

example if i was to go to  Instead of including the index.html where variable $pageincl is at it will just show the index.html as normal.

The htaccess looks like this.

Options +FollowSymlinks

# Avoid 500 error by passing charset
AddDefaultCharset utf-8

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.blah\.com [nc]
RewriteRule ^(.*)$$1 [L,r,nc]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?pageincl=$1 [QSA]

Question by:jags2ooo
    LVL 6

    Expert Comment

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    These will only fire if the requested location does not exist as a file (!-f) or a directory (!-d)

    As the index.html file exists, it is shown. Delete the index.html file and it will work.

    You'll get:

    and you'll get:


    Author Comment

    ...  Umm let me try to re-explain   i go to    instead of INcluding the Index.html file that is in people directory it will show Show me the file normally.     I would like it to include the index.html file that is in people if i go to use

    Example won't include index.php file:::::::::

    example2:  will include a non Index file

    (it looks nasty cause the content is not made for that layout lol don't mind it :D just for testing)
    LVL 6

    Accepted Solution

    Apache has a "DirectoryIndex" directive which tells Apache which file to use as a directory "index" if the HTTP request is for a directory (i.e. http://host.domain.tld/directory) rather than a specific file within a directory (i.e. http://host.domain.tld/directory/markup.html).
    (e.g. DirectoryIndex index.html)

    If the HTTP request is for a valid existing directory (remember ModRewrite will not redirect if the request matches an existing directory), Apache will look within that directory for a valid index file. If found (again, ModRewrite will not fire if that index file actually exists), Apache will server up that file.

    So, the reason that you cannot use an index.php file is that when you request the directory without specifying a file, Apache interprets that as a request to serve the index file. (e.g. http://host.domain.tld/directory get interpreted as http://host.domain.tld/directory/index.html)

    Now, if the DirectoryIndex directive does not have "index.php" in the list, then apache will not look for that file and the request, because it is interpreted as having an index file, will go unmatched thus allowing ModRewrite to take over.

    So your options are:
    1) rename the file from index.php to something else other than "index"
    2) remove index.php from the DirectoryIndex directive.

    Should you opt for #2, keep in mind that you can have more than one DirectoryIndex directive and they are allowed in all server contexts (i.e. server config, virtual host, directory, .htaccess)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now