Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to issue and customize .htaccess for Laravel?

I have just launched a Laravel 5.3 project on a live server and all is going well except one.

But my issue is, My websites are running on all 3 different URL.

My server is Ubuntu 16.4.

  1. website.com

  2. website.com/public/index.php

  3. website.com/ any word /index.php

My .htaccess is: (This file is in the root folder)

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteRule ^(.*)$ public/index.php$1 [L]
</IfModule>

But I just want to see my website with website.com URL only.

like image 854
mySun Avatar asked Feb 28 '17 08:02

mySun


3 Answers

Seems like your Laravel app is accesible via an Apache HTTP alias. Follow these steps

Try to navigate to your expected route prepend it with /index.php/, in your case: website.com/index.php/users. If it works (no 404) then you problem is with the Rewrite Module configuration of Apache HTTP, you should follow the next steps.

Edit the file public/.htaccess.

Under the line RewriteEngine On add RewriteBase / if files are on root directory if in folder like laravel then add RewriteBase /laravel/

Try to navigate to an existing route.

i.e

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ /$1 [L,R=301]

    RewriteRule ^(.*)$ public/index.php$1 [L]
</IfModule>

Remove /public from your Laravel create a virtual host

go to /etc/apache2/sites-available create .conf file or update 000-default.conf

<VirtualHost *:80>
   ServerAdmin [email protected]
   ServerName website.com
  ServerAlias www.website.com

DocumentRoot /var/www/html/index.html

        <Directory /var/www/html/>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>


        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine on
RewriteCond %{SERVER_NAME} =website.com [OR]
RewriteCond %{SERVER_NAME} =www.website.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,QSA,R=permanent]
</VirtualHost>
like image 57
M Arfan Avatar answered Oct 16 '22 12:10

M Arfan


To only match the homepage, i.e. http://domainxyz.com/ you need to match the empty path (as mod_Rewrite removes the leading /.

RewriteCond %{HTTP_HOST} ^www.domainxyz.com [NC]
RewriteRule ^$ /view.php?page=process/ [NC,L]
like image 28
lax1089 Avatar answered Oct 16 '22 14:10

lax1089


Try using this in your .htaccess file:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^website.com/([a-zA-Z-]*)/index.php [NC,OR]
RewriteCond %{HTTP_HOST} ^www.website.com/([a-zA-Z-]*)/index.php [NC]
RewriteRule ^(.*)$ http://www.website.com [L,R=302,NC]

Make sure you clear your cache before testing this. You will notice I've used R=302, this is a temporary redirect. If the redirect works and you're happy with it, then switch that to R=301 which is a permanent redirect.

like image 37
Joe Avatar answered Oct 16 '22 13:10

Joe