使用Apache部署Vue:路由不能在历史模式下工作

时间:2018-04-03 22:29:28

标签: apache vue.js vue-router

我在使用Apache(HTTPs)部署vue应用程序(没有数据库或服务器端代码)时遇到了问题。这是我的.conf文件:

<VirtualHost *:80>
ServerAdmin myemail@gmail.com
ServerName mydomain.me
ServerAlias www.mydomain.me
DocumentRoot /var/www/mydomain.me/dist
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

<Directory /var/www/mydomain.me/dist>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
</Directory>

RewriteEngine on
RewriteCond %{SERVER_NAME} =mydomain.me [OR]
RewriteCond %{SERVER_NAME} =www.mydomain.me
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

当我访问mydomain时,正确加载了首页(使用npm run build构建的index.html),但是没有一条路由起作用(即/ portfolio)。

但是,如果我将路由器模式从history切换到hash,则一切正常(/#/portfolio),但我希望history模式保持活动状态。

提前致谢!

2 个答案:

答案 0 :(得分:2)

您的案例很棘手,因为您似乎已将RewriteCondRewriteRule重定向到HTTPS。

如果是这样的话,这里可能有用:

<强> /etc/apache2/sites-enabled/mydomain.me-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName mydomain.me
    ServerAlias www.mydomain.me
    DocumentRoot /var/www/mydomain.me/dist
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/mydomain.me/dist>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mydomain.me [OR]
    RewriteCond %{SERVER_NAME} =www.mydomain.me
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/mydomain.me/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.me/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

.htaccess /var/www/mydomain.me/dist

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

答案 1 :(得分:1)

结束了一个解决方案 - 不确定它是否是最好的解决方案,但它适用于我的情况。

<强> /etc/apache2/sites-enabled/mydomain.me-le-ssl.conf

<IfModule mod_ssl.c>
<VirtualHost *:443>
    ServerName mydomain.me
    ServerAlias www.mydomain.me
    DocumentRoot /var/www/mydomain.me/dist
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined

    <Directory /var/www/mydomain.me/dist>
        Options Indexes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mydomain.me [OR]
    RewriteCond %{SERVER_NAME} =www.mydomain.me
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/mydomain.me/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mydomain.me/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

.htaccess /var/www/mydomain.me/dist

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*) index.html [QSA,L]
</IfModule>