📜  laravel htaccess - PHP (1)

📅  最后修改于: 2023-12-03 14:43:45.303000             🧑  作者: Mango

Laravel .htaccess - PHP

为了让您的 Laravel 应用程序能够在共享主机上正常运行,您需要使用适当的 .htaccess 文件。 .htaccess 文件将允许您配置 Apache web 服务器以展示您的网站。

基础 .htaccess 配置

在你的 Laravel 项目的根文件夹中创建一个名为 .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]

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

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/plain
    AddOutputFilterByType DEFLATE text/html
    AddOutputFilterByType DEFLATE text/xml
    AddOutputFilterByType DEFLATE text/css
    AddOutputFilterByType DEFLATE application/xml
    AddOutputFilterByType DEFLATE application/xhtml+xml
    AddOutputFilterByType DEFLATE application/rss+xml
    AddOutputFilterByType DEFLATE application/javascript
    AddOutputFilterByType DEFLATE application/x-javascript
    AddOutputFilterByType DEFLATE image/svg+xml
    AddOutputFilterByType DEFLATE image/x-icon
    AddOutputFilterByType DEFLATE application/json
    AddOutputFilterByType DEFLATE application/ld+json
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE font/ttf
    AddOutputFilterByType DEFLATE font/otf
    AddOutputFilterByType DEFLATE font/opentype
    AddOutputFilterByType DEFLATE font/eot
    AddOutputFilterByType DEFLATE font/woff
    AddOutputFilterByType DEFLATE font/woff2
    AddOutputFilterByType DEFLATE application/font-woff
    AddOutputFilterByType DEFLATE application/font-woff2
    AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
</IfModule>

<IfModule mod_expires.c>
    ExpiresActive on

    ExpiresDefault "access plus 1 month"

    # Cache Control for Fonts
    ExpiresByType font/ttf "access plus 1 month"
    ExpiresByType font/otf "access plus 1 month"
    ExpiresByType font/opentype "access plus 1 month"
    ExpiresByType font/eot "access plus 1 month"
    ExpiresByType font/woff "access plus 1 month"
    ExpiresByType font/woff2 "access plus 1 month"
    ExpiresByType application/font-woff "access plus 1 month"
    ExpiresByType application/font-woff2 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject "access plus 1 month"

    # Cache control for GIF, PNG, JPG and PDF
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType application/pdf "access plus 1 month"
</IfModule>

此基础 .htaccess 配置将允许 Laravel 应用程序正常运行,并且具有一些缓存和性能优化。

如何使用 .htaccess 配置?

在 Laravel 项目中使用 .htaccess 配置,您可以通过 FTP 或 shell 将其上传到项目根目录上的 Web 服务器。确保将其命名为 .htaccess,否则 Apache 将无法识别它。

总结

在共享主机上运行 Laravel 应用程序需要正确的 .htaccess 文件的配置。基础配置提供了一些性能优化,但还有其他自定义配置选项。

学习更多关于 .htaccess 配置的信息,请查看 Apache 的官方文档。