适配 .htaccess 文件以到达路由

Adapt .htaccess file to reach route

我在 apache/ubuntu 20 服务器上使用 laravel 6 和 运行。

我的 apache 主机配置如下所示:

<VirtualHost *:80>

    ServerAdmin webmaster@localhost
    DocumentRoot /home/admin/Desktop/Code

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


</VirtualHost>

我的 apache2.conf 如下所示:


Mutex file:${APACHE_LOCK_DIR} default

PidFile ${APACHE_PID_FILE}

Timeout 300

KeepAlive On

MaxKeepAliveRequests 100

KeepAliveTimeout 5

User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

HostnameLookups Off

ErrorLog ${APACHE_LOG_DIR}/error.log

LogLevel debug

IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf

Include ports.conf

<Directory />
    Options Indexes FollowSymLinks Includes ExecCGI
    AllowOverride All
    Require all granted
        Allow from All
</Directory>

<Directory /usr/share>
    AllowOverride None
    Require all granted
</Directory>

<Directory />
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

AccessFileName .htaccess

<FilesMatch "^\.ht">
    Require all denied
</FilesMatch>

LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent

IncludeOptional conf-enabled/*.conf

IncludeOptional sites-enabled/*.conf

因为我 运行正在 laravel 开发机器上,所以我不想更改我的文档根目录以指向项目。

我的 laravel 应用程序中的路线如下所示:

web.php

Route::get('/', 'IndexController@index');
Route::get('company/{symbol}', 'CompanyController@index');

在我的 .env 文件中,APP_URL 如下所示:

APP_URL=http://localhost/laravel_app/public

我可以通过以下方式到达我的基地 url /:

http://localhost/laravel_app/public/

当我从 / 转到另一个细节时 url f.ex。 http://localhost/laravel_app/public/company/AAPL,我得到:

我的 laravel_app/public/ 文件夹中的 .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>

在我的 laravel_app/ 文件夹中,我的 .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>

php_flag display_startup_errors on
php_flag display_errors on
php_flag html_errors on
php_flag  log_errors on

我尝试设置这样的路由 Route::get('company', 'CompanyController@index'); 并打开 http://localhost/laravel_app/public/company,但我什至没有用我的调试器跳转到我的 CompanyController。我收到 404 错误。

因此我在想我的 .htaccess 文件可能是错误的。

关于如何调整我的 .htaccess 文件以正确打开像 /company/{symbol} 这样的 url 有什么建议吗?

感谢您的回复!

对于默认值为 noneRewriteBaseDocumentRoot 是错误的:

DocumentRoot /home/admin/Desktop/Code

这应该是:

DocumentRoot /home/admin/Desktop/Code/laravel_app/public

然后目录 /home/admin/Desktop/Code/laravel_app/public 中的默认 .htaccess 文件应该处理重写 - 而不必更改 RewriteBase 的值,(这是另一个选项:RewriteBase "laravel_app/public").将 ServerName / ServerAlias 添加到 VirtualHost 并将主机名添加到 /etc/hosts 通常比将路径保留在 URL 中更好。否则,唯一的虚拟主机是默认主机,它应该是行中的最后一个主机,以便捕获所有不匹配的主机名(它们按文件名的顺序加载)。

看到routing; it's all done in PHP, at least while the Laravel application's main entry-point index.php is being reached (xdebug断点只能暂停执行,而有执行。