Access Laravel Site Without “Public” Folder Using Alias in AWS EC2

Recently, I had an issue with laravel project settings, especially to get rid of public and index.php from url to access the site. For example, http://example.com/mylaravel/public/challenges, or, http://example.com/mylaravel/index.php/challenges, etc.

I came through some solutions from StackOverflow or whatever, and they suggested me to use .htaccess, apache rewrite, or aliasing, etc. Some gave good solution, and some others not. Although they somehow worked, I felt a bit tempting to know the best practices, especially to prevent  attacher/hacker.

So overall, I received two recommendations: using .htaccess or aliasing in apache configurations (please let me know if anyone of you know any better options, that would be much appreciated). However, I will only put my solution here.

  1. Edit apache configuration
    1. find the configuration in /etc/httpd/conf/httpd.conf, and add lines below:
      • Alias /my_laravel "/var/www/html/laravel_project/public"

        <Directory “/var/www/html/laravel_project/public”>
        Options Indexes FollowSymLinks MultiViews
        AllowOverride all
        Order allow,deny
        Allow from all
        </Directory>

    2. restart apache: sudo httpd service restart
  2. Edit .htaccess in public folder, add lines below:<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
    Options -MultiViews
    </IfModule>RewriteEngine On

    RewriteBase /my_laravel

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
    </IfModule>

  3. Done, now we can directly use http://example.com/my_laravel, and call any controller with http://example.com/my_laravel/my_controller, without public or index.php

Basically, in step 1, we let apache to directly jump to public folder, so it will prevent any access to the upper level. While in step 2, there should be rewrite base to let the redirection works, and add rewrite rule to remove index.php.

I hope this also helps any of you who read this article. Please let me know if you have better alternatives 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s