acedened
acedened

Reputation: 81

Apache Alias / PHP / Laravel oddity

The problem description is very specific, but i have no idea about its source

  1. The project uses Laravel 5.1

  2. I have a project copy on my computer and remote server. Local server is configured to remove /public from URL with VirtualHost. On remote server, project is located in subdirectory. E.g. /project. To get rid of public, there is an alias in httpd.conf and line RewriteBase /project in /project/public/.htaccess

  3. The problem appears when using Laravel's pagination mechanism:

    There are links to pages in the page with navigation. On local machine everything is OK, but on remote server clicking on link with URL like http://<server_ip>/project/navigationPage?page=N causes redirect to http://<server_ip>/navigationPage?page=N. Of course, 404 error appears.

What is the problem and how to fix it?

Full .htaccess in /project/public:

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

RewriteEngine On

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

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

Upvotes: 1

Views: 397

Answers (1)

acedened
acedened

Reputation: 81

Ok, problem was in slash that was added to links by paginator. Link was looking like http:///project/navigationPage/?page=N, but must look like http:///project/navigationPage?page=N (without slash). Bug fixed by editing AbstractPaginator's url function:

public function url($page)
{
    if ($page <= 0) {
        $page = 1;
    }

    $parameters = [$this->pageName => $page];

    if (count($this->query) > 0) {
        $parameters = array_merge($this->query, $parameters);
    }

    return  rtrim($this->path, '/').'?'
                    .urldecode(http_build_query($parameters, null, '&'))
                    .$this->buildFragment();
}

Upvotes: 1

Related Questions