skip to Main Content

I have a site in the Apache, that the folder is:

/var/www/html/content

at this level I have an index.html

/var/www/html/content/index.html

and I have a lot of subfolders, but not all of them has a index.html

so I want if someone go to: http://example.com/subfolder/subfolder, and this subfolder does not contain an index.html, redirect to: http://example.com/

I tried with an .htaccess file in the content folder, with this rule:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /index.html [L]

but Apache shows me this:

Internal Server Error

2

Answers


  1. Your rewire conditions are saying only rewrite to index.html if the request is for a file or folder that does not exist i.e. anyone that requests a file or folder that exists will prevent the rewrite rule from activating, thus very easily bypassing what you are trying to do.

    Anyway your going about the thing the wrong way in the first place, what you should be doing is restricting access to all directories and then only allowing access into the directories that you want users to get into. Placing index files in directories is just a fail safe for incompediant developers who can’t configure apache restrictions correctly.

    Login or Signup to reply.
  2. RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^.*$ /index.html [L]
    

    This would result in an Internal Server Error (500 response) if mod_rewrite is not enabled on your server, or /index.html does not exist (which would seem unlikely given by what you’ve stated in the question).

    However, you don’t seem to need mod_rewrite at all here.

    If you want to serve index.html from the directory being requested (if it exists), otherwise default to serving /index.html from the document root (no external redirect) then you can simply modify the DirectoryIndex. For example:

    DirectoryIndex index.html /index.html
    

    The DirectoryIndex directive lists the files to try when requesting a directory. In this case, index.html refers to a file in the currently requested directory. If this does not exist then it tries /index.html (a root-relative URL) in the document root.

    Reference:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search