Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make web root folder a sub-folder with .htaccess?

I'm working on a pretty complex project, so I'd like to go with a file/folder structure that makes sense.

The folder/file structure I'd like to have is:

.htaccess
/php/
/assets/

I'd like to have the web pages that people will access in:

/php/views/pages/

I'm wondering if it's possible to use just .htaccess to set /php/views/pages as the "viewable root", so that for example, when people visit http://mydomain.com/ they'll be viewing http://mydomain.com/php/views/pages/index.php, and if so, how would I go about doing it?

Also, is it possible to canonicalize all access to /php/views/pages using .htaccess and a 301 redirect to stop multiple links being indexed in search engines?

like image 866
Avicinnian Avatar asked Apr 28 '12 15:04

Avicinnian


People also ask

How do I redirect a root to a subfolder?

You can redirect all requests to a subdirectory by adding an . htaccess file to the root of your domain's directory: Visit the FTP page for instructions on how to upload. Once connected, upload (or create) a text file named .

Does htaccess work on subdirectories?

htaccess file are applied to the directory in which the . htaccess file is found, and to all subdirectories thereof. However, it is important to also remember that there may have been . htaccess files in directories higher up.

Is a root folder located in a subfolder?

Dane Parchment. The root folder, is the folder where your project itself lies, sub-folders are directories within that root folder.


1 Answers

If you don't have access to your host configuration

DocumentRoot can only be used in server and virtual host configs, .htaccess it must be.

  1. Let's add directives in your .htaccess

    RewriteEngine on
    
  2. I assume you want to let the requests to /assets go through

    #if a match for asset is found, do nothing
    RewriteRule ^assets/ - [L]
    
  3. If a request tries to access /php/views/pages/ directly, redirect it to its canonical version

    RewriteCond %{THE_REQUEST} php/views/pages/
    RewriteRule ^php/views/pages/(.*) http://mydomain.com/$1 [R=301,L]
    
  4. And add a rule to map everything else to /php/views/pages/

    RewriteCond %{REQUEST_URI} !php/views/pages/
    RewriteRule ^(.*)$ /php/views/pages/$1 [L]
    

If you have access to your host configuration

Forget about .htaccess files and use it. A sample configuration could look like

# ~base stands for your original DocumentRoot
<VirtualHost *:80>
    DocumentRoot ~base/php/views/pages
    Alias /assets ~base/assets

    # other configuration parameters
</VirtualHost>
like image 107
nikoshr Avatar answered Oct 24 '22 21:10

nikoshr