You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

共享主机Laravel 4.2应用本地部署后重定向至原URL的解决咨询

问题描述

将共享主机上的Laravel 4.2应用部署到本地Ubuntu 16.04 + Apache2 + MySQL环境,已完成数据库连接配置修改,artisan迁移命令可正常操作本地数据库,但访问localhost时页面会被重定向至原网站URL。

已修改app/config/app.php中的url配置项为localhost,但重定向问题仍存在。

相关配置信息:

Apache虚拟主机配置(/etc/apache2/sites-available/laravel.conf)

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName laravel.example.com
    DocumentRoot /var/www/html/public_html/
     
    <Directory /var/www/html/public_html/>
            AllowOverride All
            Require all granted
    </Directory>
     
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

项目目录结构

.
├── app
│   ├── commands
│   ├── config
│   ├── controllers
│   ├── database
│   ├── filters.php
│   ├── helpers
│   ├── lang
│   ├── models
│   ├── public_html.tar.gz
│   ├── routes.php
│   ├── service
│   ├── start
│   ├── storage
│   ├── tests
│   └── views
├── artisan
├── bootstrap
│   ├── autoload.php
│   ├── paths.php
│   └── start.php
├── composer.json
├── composer.lock
├── index.html
├── public_html
│   ├── 400.shtml
│   ├── 401.shtml
│   ├── 403.shtml
│   ├── 404.shtml
│   ├── 413.shtml
│   ├── 500.shtml
│   ├── assets
│   ├── cp_errordocument.shtml
│   ├── error_log
│   ├── favicon.ico
│   ├── index.php
│   ├── info.php
│   ├── less
│   ├── packages
│   ├── robots.txt
│   └── upload

原public_html/.htaccess内容

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.tenispremium.ro/$1 [R,L]

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

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]

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

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

重定向问题的核心原因是.htaccess顶部的强制跳转规则,直接将所有80端口的请求转发到原网站域名。修改.htaccess步骤如下:

  1. 删除强制跳转至原域名的规则
    移除文件最顶部的以下代码块:

    RewriteEngine On 
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ https://www.tenispremium.ro/$1 [R,L]
    
  2. 清理本地不需要的配置
    本地环境不需要cPanel生成的PHP handler配置,可注释或删除以下代码块:

    # php -- BEGIN cPanel-generated handler, do not edit
    # Set the “ea-php70” package as the default “PHP” programming language.
    <IfModule mime_module>
      AddHandler application/x-httpd-ea-php70 .php .php7 .phtml
    </IfModule>
    # php -- END cPanel-generated handler, do not edit
    
  3. 保留Laravel核心重写规则
    保留以下Laravel默认的URL重写规则即可:

    <IfModule mod_rewrite.c>
        <IfModule mod_negotiation.c>
            Options -MultiViews
        </IfModule>
    
        RewriteEngine On
    
        # Redirect Trailing Slashes...
        RewriteRule ^(.*)/$ /$1 [L,R=301]
    
        # Handle Front Controller...
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [L]
    </IfModule>
    
  4. 额外配置建议

    • 确认Apache的mod_rewrite模块已启用,执行命令:sudo a2enmod rewrite && sudo systemctl restart apache2
    • 在本地/etc/hosts文件中添加一行:127.0.0.1 laravel.example.com,之后通过laravel.example.com访问应用,更匹配虚拟主机配置。

内容的提问来源于stack exchange,提问作者Cristian Cutite

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 15:13:17