doucuan5365 2015-06-25 22:18
浏览 100

Apache重写规则与Laravel无法正常工作

I have the following directory structure:

/home/username/public_html
        my_laravel_app/
            .htaccess
            app/
            bootstrap/
            public/
                .htaccess
                index.php
                robots.txt
                ...etc (standard Laravel 4 public folder contents)
            vendor/
            ...etc (standard Laravel 4 files and folders)

I am trying to use an apache RewriteRule to redirect a request to http://example.com/~user/my_laravel_app to http://example.com/~user/my_laravel_app/public/index.php. In the first .htaccess file (the one directly under the my_laravel_app/ directory, I have the following code:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /~username/my_laravel_app
        RewriteCond %{THE_REQUEST} !public/
        RewriteRule ^(.*)$ public/index.php [R]
</IfModule>

When I navigate my browser to http://example.com/~username/my_laravel_app, I correctly get redirected to http://example.com/~user/my_laravel_app/public/index.php, and the laravel application loads as expected displaying the route for / as defined in app/routes.php.

However, I want the redirect to be internal, meaning, I don't want the users browser to display the new location. So, I simply remove the [R] flag on the rule. The .htaccess file now contains:

<IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /~username/my_laravel_app
        RewriteCond %{THE_REQUEST} !public/
        RewriteRule ^(.*)$ public/index.php
</IfModule>

Now, when I direct the browser to http://example.com/~username/my_laravel_app I get a Laravel whoops error page saying there was a NotFoundHttpException. When I look at the error page, it seems as though the redirect is "working", because I see that the SCRIPT_NAME is /~username/my_laravel_app/public/index.php.

However, I believe the issue is possibly that the REQUEST_URI is /~username/my_laravel_app/, instead of '/', as it would be if Laravel were installed in the root of a domain like normal. So it seems as though Laravel is looking for a route match using this REQUEST_URI variable.

Is there any way to get Laravel to recognize the correct route?

  • 写回答

1条回答 默认 最新

  • dqba94394 2015-07-19 21:22
    关注

    I had the same issue on Laravel 4.1, and appears related to my apache config, however, I did find a workaround which was to add the following to the top of the public/index.php:

    $_SERVER['SCRIPT_NAME'] = 'path/to/laravel/index.php';

    // note: this is a workaround for mod_rewrite and laravel which updates the script_name from /path/to/laravel/public/index.php to /path/to/laravel/index.php. I forget exactly why this worked, but I think when I traced the problem back it was related to something in a symfony dependency.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画