How to release and configure .htaccess for Laravel?

I just launched the Laravel 5.3 project on a real server and everything is going fine except one.

But my problem is that my websites work on all three different URLs.

My Ubuntu Server 16.4.

  • website.com

  • website.com/public/index.php

  • website.com/ any word /index.php

My .htaccess : (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 site only with website.com URL.

+7
php apache .htaccess laravel
source share
5 answers

It looks like your Laravel application is accessible through the Apache HTTP alias. Follow these steps.

Try navigating to your expected route by adding it to /index.php/ , in your case: website.com/index.php/users . If it works (no. 404), then there is a problem with configuring the Rewrite Module Apache HTTP module, you must follow the following steps.

Edit the public / .htaccess file.

In the RewriteEngine On line, add RewriteBase / if the files are in the root directory, if in a folder, for example laravel , then add RewriteBase /laravel/

Try moving to an existing route.

ie

 <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> 

Delete / publish from your Laravel create virtual host

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

 <VirtualHost *:80> ServerAdmin admin@example.com 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> 
+2
source share

To match only the home page, i.e. http://domainxyz.com/ , you need to map an empty path (since mod_Rewrite removes the leading / .

 RewriteCond %{HTTP_HOST} ^www.domainxyz.com [NC] RewriteRule ^$ /view.php?page=process/ [NC,L] 
+1
source share

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] 

Before testing, make sure you clear the cache. You will notice that I used R=302 , this is a temporary redirect. If the redirection works, and you are happy with it, switch it to R=301 , which is a constant redirect.

+1
source share

This code worked for me. It will remove the extension from your site, for example .php and that’s it. Try it once.

 Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / RewriteCond %{THE_REQUEST} ^[AZ]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R=301,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*?)/?$ $1.php [NC,L] 
+1
source share

1. I understand why you are trying to change .htaccess, but this can be solved much easier. Revert .htaccess file to Laravel standard

 <IfModule mod_rewrite.c> <IfModule mod_negotiation.c> Options -MultiViews </IfModule> RewriteEngine On # Redirect Trailing Slashes If Not A Folder... RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # Handle Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule> 

2. Move all your folders and files separately from the public ones and make sure that they are located behind your HTML folder.

3. Move all your shared folders and files from the shared folder and to your html folder

4. Tell laravel to look in your html folder instead of public by changing the server.php file

 <?php /** * Laravel - A PHP Framework For Web Artisans * * @package Laravel * @author Taylor Otwell < taylorotwell@gmail.com > */ $uri = urldecode( parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) ); // This file allows us to emulate Apache "mod_rewrite" functionality from the // built-in PHP web server. This provides a convenient way to test a Laravel // application without having installed a "real" web server software here. if ($uri !== '/' && file_exists(__DIR__.'/html'.$uri)) { return false; } require_once __DIR__.'/html/index.php'; 

I find this solution much easier than htaccess talking to you. You also warrant that all major Laravel files cannot be accessed from the URL.

Another problem you are facing (inner pages)

This is probably due to the fact that your apache does not allow overriding with Laravels htaccess

Go to the command line of your server and get access to the apache2.conf file

 cd /etc/apache2 

Open apache2.conf file with nano

 sudo nano apache2.conf 

Now find the following

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

Change it to

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

Record the changes and exit nano.

Restart apache

 sudo service apache2 restart 

Now it should work :)

+1
source share

All Articles