user801172
user801172

Reputation: 21

htaccess mod_rewrite : Redirect root to folder

When I call the root of my webpage I got error 403:

Forbidden You don't have permission to access / on this server.

My htaccess looks like:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/$

RewriteRule (.*) dir/file.php

Options -Indexes

How can I redirect the root to a folder, with no error 403? And without loosing the functionality of

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

Upvotes: 1

Views: 3590

Answers (2)

user801172
user801172

Reputation: 21

To redirect everything, except:

  • existing files
  • existing dirs with an index.php in it

.htaccess content:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /special/index.php
RewriteRule ^$ /special/index.php [QSA,L]
Options -Indexes

Upvotes: 1

LazyOne
LazyOne

Reputation: 165148

Most likely (based on my experience) you do not have default file that will be executed when directory is requested.

1) You may need to add this directive into your .htaccess. This will tell Apache to execute index.php when requesting directory.

DirectoryIndex index.php

2) Make sure you do have index.php present in such directory, otherwise you will see the 403 error.

What exactly do you mean by "How can I redirect the root to a folder"? Here is the rule that will execute /special/index.php file if you request root folder of your site (e.g. http://example.com/

RewriteRule ^$ /special/index.php [QSA,L]

UPDATE:

RewriteRule ^mydir/(index\.php)?$ http://www.example.com/ [R=301,QSA,NC]

This will tell Apache to redirect (change URL in a browser's address bar) from http://www.example.com/mydir/index.php or http://www.example.com/mydir/ to http://www.example.com/

RewriteRule ^$ mydir/index.php [QSA,L]

This will tell Apache to execute /mydir/index.php when you hit the root folder (e.g. http://www.example.com/) without changing URL in address bar.

If you want it to work together with your existing rules you will have to put these 2 rules above other -- for example, right after RewriteBase /

Upvotes: 0

Related Questions