【问题标题】:How to issue and customize .htaccess for Laravel?如何为 Laravel 发布和自定义 .htaccess?
【发布时间】:2017-07-19 02:34:01
【问题描述】:

我刚刚在实时服务器上启动了一个 Laravel 5.3 项目,除了一个之外一切都很好。

但我的问题是,我的网站在所有 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 的网站。

【问题讨论】:

  • 嗨,您想从您的网站中删除 /public ... 吗?
  • 嗨,如果我错了,请纠正我。您希望从 example.com 而不是 www.example.com 提供所有 url。并且所有以 /index.php 结尾的 URL 都被重定向到 example.com?

标签: php apache laravel .htaccess laravel-5.3


【解决方案1】:

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。将所有文件夹和文件从 public 中移开,并确保它们位于 HTML 文件夹后面。

3.将所有公用文件夹和文件从公用文件夹移到 html 文件夹中

4.通过更改 server.php 文件告诉 laravel 查看你的 html 文件夹而不是公共文件夹

<?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'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 简单得多。您还要确保不能从 url 访问所有 Laravel 核心文件。

您遇到的其他问题(内部页面)

这可能是因为你的 apache 不允许用 Laravel htaccess 覆盖

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

cd /etc/apache2

用 nano 打开 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>

写出更改并退出 nano。

重启apache

sudo service apache2 restart

它现在应该可以工作了 :)

【讨论】:

    【解决方案2】:

    这段代码对我有用。它将从您的网站中删除扩展名,例如 .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]
    

    【讨论】:

    • 嗨,我已将此代码添加到 public/.htaccess 或 root 中的 '.htaccess` 中?
    • 你可以在这两个地方添加这个。但 public/.htaccess 是我的建议。
    • 感谢您的帮助 :-),我在 public/.htaccess 中覆盖了这段代码。这段代码是真的,但现在我可以看到带有website.com/public 的网站:-(
    • 将此代码放入您的根 .htaccess 文件选项 -MultiViews RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ public/index.php [L]跨度>
    【解决方案3】:

    您的 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 中移除 /public 创建虚拟主机

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

    【讨论】:

    • 嗨,谢谢你的帮助,我在根文件夹和公共文件夹的.htaccess 文件中添加了RewriteBase /。只有这个是真的:website.com/ any word /index.php 现在我可以用website.com/public/index.php 打开网站:-(
    • @mySun 现在工作正常吗.. 你还有问题吗..?
    • 我的示例代码在根项目的.htaccess 文件中。现在在RewriteEngine On 行下,在文件public/.htaccess. 中添加RewriteBase /。但我可以通过website.com/public/index.phpwebsite.com/public 看到我的网站。
    • @mySun 我已经更新了我的答案,它可能对您有更多帮助.. 删除您的 URL 的公共形式.. 如果您仍然无法删除公共,您可以看到这个主题 howsolve.com/blog/laravel-applications-on-shared-hosting .. 它会有所帮助你更。它用于共享主机。你也可以在linux上申请
    【解决方案4】:

    尝试在您的 .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,这是一个永久重定向。

    【讨论】:

    • 嗨,谢谢你的帮助,但不要使用这个代码:-(
    • 我的文件是这条路线:/var/www/html.
    【解决方案5】:

    要仅匹配主页,即http://domainxyz.com/,您需要匹配空路径(因为 mod_Rewrite 删除了前导 /

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

    【讨论】:

    • 嗨,我不使用例如 /view.php?page=process/ 我的框架,我的问题是看到 website.com 这个 website.com/ any word /index.php url。
    猜你喜欢
    • 2018-12-21
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 2010-11-04
    • 2014-12-16
    • 2011-09-18
    相关资源
    最近更新 更多