【问题标题】:how to remove 'public' in url when redirecting http:// to https:// in laravel using htaccess?使用htaccess在laravel中将http://重定向到https://时如何删除url中的“public”?
【发布时间】:2023-03-20 23:25:01
【问题描述】:

当我使用https://example.com 打开网站时,它会在 URL 中没有 public 的情况下打开,但是当我尝试打开 http://example.com 时,它将重定向到 https://example.com/public,而 URL 中的 public

我想将http:// 重定向到https://,但网址中没有public

文件夹结构:

.htaccess 根目录下的文件代码

#disable directory browsing
Options -Indexes
<IfModule mod_rewrite.c>
   RewriteEngine On 
   RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

#PROTECT ENV FILE
<Files .env>
order allow,deny
Deny from all
</Files>

#PROTECT ENV FILE
<Files .htaccess>
order allow,deny
Deny from all
</Files>

# BEGIN cPanel-generated php ini directives, do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file, use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information, read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
   php_flag display_errors Off
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit -1
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
   php_value upload_max_filesize 2M
   php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
   php_flag display_errors Off
   php_value max_execution_time 30
   php_value max_input_time 60
   php_value max_input_vars 1000
   php_value memory_limit -1
   php_value post_max_size 8M
   php_value session.gc_maxlifetime 1440
   php_value session.save_path "/var/cpanel/php/sessions/ea-php74"
   php_value upload_max_filesize 2M
   php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives, do not edit

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php74” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php74 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

公共目录下的.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>


【问题讨论】:

  • 请帮我找出问题所在!
  • 您发布的代码中没有 HTTP 到 HTTPS 重定向。你知道这是在哪里触发的吗? (您有权访问服务器配置吗?)重定向中使用的 3xx HTTP 状态代码是什么? “......它将重定向到https://example.com/public” - 你的意思是/public/(带有斜杠)?还是在重定向后实际上没有斜杠(这很重要)?

标签: laravel .htaccess url-rewriting laravel-8 http-redirect


【解决方案1】:

您发布的代码中没有 HTTP 到 HTTPS 重定向。如果这是在服务器配置中较早执行的,则需要在服务器配置中进行更正。但是,如果稍后在您的应用程序中执行(错误地),那么您可以通过在.htaccess 中尽早重定向来纠正此问题。这需要放在/public/.htaccess 文件的顶部:

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://example.com/$1 [R=301,L]

(这确实假设 SSL 证书直接安装在您的应用程序服务器上,并且您没有使用代理来管理您的 SSL 连接。如果是这样,则需要调整 条件。)

由于这是在/public/.htaccess 文件中,因此捕获的反向引用$1 将自然排除/public/ 子目录,因此从重定向中排除。

您应该首先使用 302(临时)重定向进行测试,以避免潜在的缓存问题。并确保您在测试前已清除浏览器缓存。

但是,如果您请求带有尾部斜杠的 URL,以下规则会出现问题,并且还会暴露 /public 子目录:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]

如果该规则位于根 .htaccess 文件中,则该规则是可以的,但是,如果它位于 /public/.htaccess 文件中(它必须是),那么 REQUEST_URI 服务器变量包含 /public/ 目录(在内部重写请求之后)。您正在从变量中捕获 URL 路径。

这条规则应该这样写:

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+)/$ /$1 [L,R=301]

这也稍微更有效并且无论它放在哪个.htaccess文件中都有效(尽管它应该在/public/.htaccess文件中,而不是在根.htaccess中)。 除此之外:如果/public 子目录没有从 URL 中隐藏,那么这将不起作用,但这里不是这种情况。

【讨论】:

    猜你喜欢
    • 2020-10-10
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多