简体   繁体   中英

Apache Rewrite rule with Laravel not working

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM