如何为Laravel发布和定制.htaccess?

问题描述:

我刚刚在一台实时服务器上启动了Laravel 5.3项目,除了一台以外,所有项目都进展顺利。如何为Laravel发布和定制.htaccess?

但我的问题是,我的网站在所有3个不同的URL上运行。

我的服务器是Ubuntu 16.4。

  1. website.com

  2. website.com/public/index.php

  3. website.com/任何单词的index.php

.htaccess是:(此文件位于根文件夹中)

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

但我只想看到我的网站只有website.com的URL。

+0

是喜是要删除/公共形成你的网站...? –

+0

嗨,纠正我,如果我错了。您希望您的所有网址都来自example.com,而不是www.example.com。除了那些以/index.php结尾的url之外的所有url将被重定向到example.com? – TheChetan

要仅http://domainxyz.com/需要匹配空路径(如mod_rewrite的删除前导/匹配的网页,即

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

嗨,我不使用例如'/view.php?page=process/'我的框架,我的问题是看到'website.com'这个'website.com/任何word/index.php'网址。 – mySun

尝试在你的.htaccess文件中使用这样的:

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] 

制作确定你在测试之前先清空你的缓存,你会注意到我已经使用了R=302,这是一个临时重定向,如果重定向工作,并且你对它满意,那么把它切换到R=301这是一个永久的重定向。

+0

嗨,谢谢你的帮助,但不要工作这段代码:-( – mySun

+0

我的文件是这样的路线:'/ var/www/html'。 – mySun

似乎您的Laravel应用程序可通过Apache HTTP别名访问。请按照这些步骤

尝试导航到您的预期路线前加上/index.php/,在您的情况:website.com/index.php/users。如果它工作(不404),那么你的问题是Apache HTTP的重写模块配置,你应该按照下面的步骤。

编辑文件public/.htaccess。

在订单RewriteEngine On添加RewriteBase /如果文件是在根目录下,如果在像laravel文件夹,然后添加RewriteBase /laravel/

尝试导航到一个现有的航线。

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

卸下/公众从Laravel 创建一个虚拟主机

/etc/apache2/sites-available创建.conf文件或更新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> 
+0

嗨,谢谢你的帮助,我在'RewriteBase /'中加入'.htaccess'文件在根文件夹和公共文件夹中。只有这样的情况下,true:'website.com/ any word/index.php',现在我可以用'website.com/public/index.php'打开网站:-( – mySun

+0

@mySun现在工作正常..你仍然有问题..? –

+0

我的示例代码是在根项目的'.htaccess'文件中。现在在'RewriteEngine On'行下面添加'RewriteBase /'文件' public/.htaccess.',但我可以通过网站看到我的网站。com/public/index.php'和'website.com/public'。 – mySun

此代码为我工作。它会消除您网站的延伸,例如。PHP和所有。尝试一次。

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

嗨,我已经添加此代码到'公共/ .htaccess'或'.htaccess'在根? – mySun

+0

您可以在两个地方添加此项。但公共/ .htaccess是我的建议。 – Siddhesh

+0

我有追加这个代码或覆盖? – mySun

1.我明白为什么你试图改变的.htaccess但是这可以解决很多简单。恢复.htaccess文件回到Laravel标准

<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.将您的所有文件夹和文件的除了公众,并确保他们的背后你的HTML文件夹中。

3.把你所有的公共文件夹和文件进行公用文件夹的和你的HTML文件夹

4.泰尔laravel通过改变服务器查找在HTML文件夹,而不是公共的。 PHP文件

<?php 

/** 
* Laravel - A PHP Framework For Web Artisans 
* 
* @package Laravel 
* @author Taylor Otwell <[email protected]> 
*/ 

$uri = urldecode(
    parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) 
); 

// This file allows us to emulate Apache's "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'; 

我觉得这个解决方案不是与你的htaccess乱搞简单得多。你也可以确保所有的Laravel核心文件都不能通过url访问。

其他问题你有(内部页)

这可能是由于你的Apache不允许覆盖与Laravels htaccess的

转到您的服务器的命令行和访问apache2.conf文件

cd /etc/apache2 

纳米打开apache2.conf文件

sudo nano apache2.conf 

现在找到以下

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

更改为

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

WriteOut的更改并退出nano。

重新启动Apache

sudo service apache2 restart 

现在应该工作:)