Dhairya Lakhera
Dhairya Lakhera

Reputation: 4808

Apache Document Root using .htaccess

I have a directory structure

mymvc
    |--App
    |--Core
    |--logs
    |--public
             |--index.php
    |--vendor
    |--.htaccess

what i want is that if someone hit my url www.example.com/mymvc/ then all the request must go through public->index.php using .htaccess file. i do not have access to httpd.conf file.

|--public
        |--index.php

I want my public folder to be accessible only as a document root and request pass through index.php file inside public folder. No one can access directly App , Core , logs etc. directories. Means i want my public folder to be DOCUMENT ROOT.

Upvotes: 3

Views: 4661

Answers (1)

Dhairya Lakhera
Dhairya Lakhera

Reputation: 4808

First you need to create an .htaccess file in the root of the project.

mymvc/.htaccess

RewriteEngine On
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) public/$1 [L]

Then, in the .htaccess file in the public directory (which would be mymvc/public/.htaccess), you need to add a RewriteBase directive to the existing code, so it looks like this:

mymvc/public/.htaccess

# Remove the question mark from the request but maintain the query string
RewriteEngine On
RewriteBase /mymvc
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

Upvotes: 5

Related Questions