【问题标题】:How do I by-pass the Public Folder in the url (Laravel 5.4)如何绕过网址中的公用文件夹(Laravel 5.4)
【发布时间】:2018-07-31 23:48:58
【问题描述】:

我已经阅读并尝试了不同的解决方案来解决这个问题,包括这篇文章Laravel 5 – Remove Public from URL 中的解决方案,但它对我不起作用。 我有一个名为“abimswake”的文件夹 - 根文件夹。每次我必须访问我的项目时,我都会通过这个 url http://localhost:8080/abimswake/public/login 访问 在尝试了该帖子中的所有解决方案后,当我尝试访问相同的 URL 时,我的 url 更改为 http://localhost:8080/public/public

然后我也想知道没有公众的网址会是什么样子。请有人帮助我。我的 .htaccess 文件在这里

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

    RewriteRule ^(.*)$ public/$1 [L]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

【问题讨论】:

  • Laravel 的 public 文件夹应该是你服务器的 DocumentRoot
  • “没有公众的 url 会是什么样子”是什么意思? http://localhost:8080/publichttp://localhost:8080/ 呢?

标签: php .htaccess laravel-5


【解决方案1】:

更新您的 Web 服务器的虚拟主机以指向 abimswake/public 并使用公共目录中的默认 Laravel .htaccess 文件。

默认 htaccess

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

Apache 虚拟主机示例

<VirtualHost *:80>
  ServerName hostname-goes-here
  DocumentRoot "/path/to/abimswake/public"
  <Directory "/path/to/abimswake/public">
    AllowOverride all
  </Directory>
</VirtualHost>

Nginx 虚拟主机示例

server {
    listen 80;

    root /path/to/abimswake/public;
    index index.php index.html index.htm;

    server_name hostname-goes-here;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

这将从公共目录服务器并使用您的主机名(您目前使用 localhost,我建议创建自定义主机名)。结合默认的 .htaccess,您的 URL 中应该没有不需要的子目录。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-11
    • 2018-11-26
    • 2019-07-28
    • 2017-07-27
    • 1970-01-01
    • 2015-09-17
    • 2020-09-14
    • 2015-02-08
    相关资源
    最近更新 更多