laravel 来自vuerouter的HTML5历史模式在生产环境中不起作用

lbsnaicq  于 7个月前  发布在  HTML5
关注(0)|答案(3)|浏览(81)

在我的Laravel和Vue.js V2项目中,我实现了带有历史模式的Vue Router v3。它在我的开发机器上运行得很好。但是,当我切换到生产环境时,我收到错误消息“**请求的URL在此服务器上找不到。
我遵循了官方文档(https://v3.router.vuejs.org/guide/essentials/history-mode.html#example-server-configurations),但没有改进。
Laravel中的My .htaccess

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteRule ^index\.html$ - [L]
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.html [L]
    </IfModule>

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

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

/etc/apache2/sites-available/laravel.conf

NameVirtualHost *:8080
Listen 8080

<VirtualHost *:8080>
        # The ServerName directive sets the request scheme, hostname and port that
        # the server uses to identify itself. This is used when creating
        # redirection URLs. In the context of virtual hosts, the ServerName
        # specifies what hostname must appear in the request's Host: header to
        # match this virtual host. For the default virtual host (this file) this
        # value is not decisive as it is used as a last resort host regardless.
        # However, you must set it for any further virtual host explicitly.
        #ServerName www.example.com

        ServerAdmin [email protected]
        ServerName prod.mycompany.net
        ServerAlias laravel
        DocumentRoot /var/www/html/laravel/public

        <Directory "/var/www/html/laravel">
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
        </Directory>

        # Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
        # error, crit, alert, emerg.
        # It is also possible to configure the loglevel for particular
        # modules, e.g.
        #LogLevel info ssl:warn

        LogLevel debug
        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        # For most configuration files from conf-available/, which are
        # enabled or disabled at a global level, it is possible to
        # include a line for only one particular virtual host. For example the
        # following line enables the CGI configuration for this host only
        # after it has been globally disabled with "a2disconf".
        #Include conf-available/serve-cgi-bin.conf

</VirtualHost>

# vim: syntax=apache ts=4 sw=4 sts=4 sr noet

我的Apache版本

Server version: Apache/2.4.41 (Ubuntu)
Server built:   2023-03-08T17:32:54

有人知道问题可能来自哪里吗?

xvw2m8pv

xvw2m8pv1#

你应该在你的.htaccess文件中删除index.html处理(它是否存在于/var/www/html/laravel/public?)并让Laravel路由指向将为您的Vue.js应用提供服务的控制器。

cotxawn7

cotxawn72#

你在你的路由文件(web.php)中放了一个备用路由吗?

Route::fallback(function() {
    return view('index');
});

将index替换为Vue应用的实际刀片起始点。

z2acfund

z2acfund3#

我发现了错误。它来自. htaccess。
工作的好.htaccess:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>
   
    RewriteEngine On

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

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

相关问题