skip to Main Content

I have a Laravel style MVC webapp. Not written in Laravel, but similar concept.

On my dev box, my webapp lived in its own “domain” with the /public folder being the document root.

Folder Structure

MyProject
 | 
 +-- App
 |  |  
 |  +-- Models
 |  |  
 |  +-- Views
 |  |  
 |  +-- Controllers
 |    
 +-- public (doc root)
    |  
    +-- htaccess
    +-- index.php

I used this .htaccess file to route all the traffic through the index.php file that lived in public folder.

htaccess

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

So when you visited http://myinternalproject.com, you could use the webapp fine.

Now, for production, I have to move the project and all of its files to its own folder within our company domain. It cannot be a subdomain as per the Administrator.(Which would fix all my problems.)

So it has to be accessed via www.mydomain.com/apps/myproject/.

Navigating to: www.mydomain.com/apps/myproject/public works fine and the site shows up.

Navigating beyond the “home page” causes all the links to go to: www.mydomain.com/{controller}/{action} instead of living in the new sub-folder.

Is there a way to rewrite all the links in my app to automatically go to /apps/myproject/public (since that is where the index.php lives) without going through every possible link and prefixing /apps/myproject/public?

I tried variations on appending the new folder structure to the htaccess, but can’t get it to work.

RewriteBase /apps/myproject/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /apps/myproject/public/^(.*)$ /apps/myproject/public/index.php?$1 [L,QSA]

2

Answers


  1. RewriteEngine on 
    # Change example.com to be your main domain. 
    RewriteCond %{HTTP_HOST} ^(www.)?xyz.com$ 
    # Change 'subdirectory' to be the directory you will use for your main domain. 
    RewriteCond %{REQUEST_URI} !^/subdirectory/ 
    # Don't change the following two lines. 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    # Change 'subdirectory' to be the directory you will use for your main domain. 
    RewriteRule ^(.*)$ /subdirectory/$1 
    # Change example.com to be your main domain again. 
    # Change 'subdirectory' to be the directory you will use for your main domain 
    # followed by / then the main file for your site, index.php, index.html, etc. 
    RewriteCond %{HTTP_HOST} ^(www.)?xyz.com$ 
    RewriteRule ^(/)?$ subdirectory/index.html [L]
    

    More about this here

    Login or Signup to reply.
  2. You may use this additional rule in your document root .htaccess:

    RewriteEngine On
    
    RewriteRule .* apps/myproject/public/$0 [L]
    

    Then you should keep this rule inside /apps/myproject/public/.htaccess:

    RewriteEngine On
    RewriteBase /apps/myproject/public/
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .* index.php?$0 [L,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search