【问题标题】:Setup subdirectory redirect to a Laravel project设置子目录重定向到 Laravel 项目
【发布时间】:2017-08-19 21:38:44
【问题描述】:

我有三个项目,第一个是静态 html 网站,另外两个是 Laravel 项目。

我为主网站设置了 SSL 证书。我想将其他 Laravel 项目设置为主 URL 的子目录。

我在我的 conf 文件中添加了一个别名,但它不起作用。我收到此错误:“尝试打开“https://example.com/admin/”时发生了太多重定向。如果您打开一个页面,该页面被重定向以打开另一个页面,然后该页面又被重定向以打开原始页面。”

我应该在 htaccess 文件中添加什么更改吗?

这是我的 website.conf 文件

<VirtualHost *:80>
    ServerName example.com
    Redirect permanent / https://example.com/
</VirtualHost>

<VirtualHost *:443>
    ServerName example.com
    ServerAlias www.example.com
    SSLEngine on
    SSLCertificateFile      /etc/apache2/ssl/***********.crt
    SSLCertificateKeyFile  /etc/apache2/ssl/*********.key
    SSLCertificateChainFile /etc/apache2/ssl/*********.crt

    ServerAdmin admin@example.com
    DocumentRoot /var/www/html/example/public_html/
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /admin "/var/www/html/admin"
    <Directory "/var/www/html/admin">
             Options Indexes FollowSymLinks
            AllowOverride All
            Order allow,deny
            Allow from all
    </Directory>

Alias /admin2  "/var/www/html/admin2"
<Directory "/var/www/html/admin2">
         Options Indexes FollowSymLinks
         AllowOverride All
         Order allow,deny
         Allow from all
</Directory>

【问题讨论】:

  • 使用真正的子文件夹 adminadmin2 并稍微修改 index.php 并在 docroot 之外使用 laravel 代码会更容易。无需让代码从网络上可见。

标签: apache laravel redirect


【解决方案1】:

我建议用 Laravel Homestead 预建盒子看看 Vagrant。这将通过编辑 Homestead.yaml 文件、/etc/hosts 文件,然后配置 vagrant 机器来实现您想要做的事情。您将需要安装虚拟盒才能与 vagrant 一起使用。您可以通过在 homestead 目录中键入 vagrant ssh 来 SSH 到机器。

文档:https://laravel.com/docs/5.4/homestead

关于 Vagrant 的 YouTube 视频:https://www.youtube.com/watch?v=LyIyyFDgO4o

关于安装 Laravel Homestead 的 Youtube 视频:https://www.youtube.com/watch?v=tbPGuikTzKk&list=PLfdtiltiRHWEaImCreOC7UkK4U5Xv9LdI

【讨论】:

  • 谢谢 J!我这里有个问题,主网站是一个静态的 html,所以我无法设置 Laravel 配置,直到我将它转换为 Laravel 项目。服务器机器也非常小,无法设置两个 VM。我正在寻找一种简单的解决方案来设置来自 Apache 服务器的重定向。
  • 将静态网站放入新的 l​​aravel 项目公用文件夹中。按照上面的指示。你的问题将得到解决。在安装 laravel homestead 和 vagrant 之前,还要从您的机器上删除 XAMMP 或 WAMP 或任何其他 apache 服务器和 mysql。它们会引起冲突。
【解决方案2】:

您应该使用 public 文件夹作为 apache 设置中的别名。

尝试 "/var/www/html/admin2/public" 以及目录。

类似这样的:

Alias /admin2  "/var/www/html/admin2/public"
<Directory "/var/www/html/admin2/public">
         Options Indexes FollowSymLinks
         AllowOverride All
         Order allow,deny
         Allow from all
</Directory>

取决于 Laravel 的安装,你应该在公共目录中有一个 .htaccess 文件,这可能需要一些额外的 tweeking。

另外,完成更改后重新启动 apache。t

【讨论】:

    【解决方案3】:

    你需要正确设置两件事:

    1. 您网站在 WAMP 别名文件夹中的别名,例如C:\wamp\alias 将 URL 重定向到 Laravel 文件夹中的 public\index.php
    2. 在 Laravel 的 public 文件夹中重写基础 .htaccess,以便 Apache 在基础路径之上重写。

    假设您网站的目录在C:\wamp\www\site,而您的别名是localhost/alias

    1. 设置别名指向public文件夹:
        Alias /site  "/var/www/site/public"
        <Directory "/var/www/site/public">
                 Options Indexes FollowSymLinks
                 AllowOverride All
                 Order allow,deny
                 Allow from all
        </Directory>
    
    1. 在 Laravel 的 public 文件夹中的 .htaccess 中添加 RewriteBase
        <IfModule mod_rewrite.c>
            <IfModule mod_negotiation.c>
                Options -MultiViews -Indexes
            </IfModule>
        
            RewriteEngine On
            RewriteBase '/site/'
        
            # Handle Authorization Header
            RewriteCond %{HTTP:Authorization} .
            RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
        
            # Redirect Trailing Slashes If Not A Folder...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_URI} (.+)/$
            RewriteRule ^ %1 [L,R=301]
        
            # Handle Front Controller...
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^ index.php [L]
        </IfModule>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 1970-01-01
      • 2015-11-04
      • 2015-07-30
      相关资源
      最近更新 更多