We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Double content

Hi there!

I wonder how to avoid the double content of my Phalcon projects when in path '/' and '/public/' there is the same content.

It's kinda bad for SEO, so I wonder.

Thanks in advance!

Mike

Hey friend, I was having the same problem. Tried my best to make it work with htaccess, but failed miserably, since I'm not htaccess expert.

This is my old htaccess post and David's suggestion.

For now I've made a PHP solution in my "bootstrap" file, until I find a better one. It's working, so if you fail with htaccess too, you can use it :)

Sample url for our test: https://localhost/framework-v1.0/public/api/v1/documentation/news


$redirect = false;

// 301 Redirect if /public/index.php (this only shows homepage so just redirect to real homepage)
$currentUrl = getCurrentUrl(); // My custom function which returns: https://localhost/framework-v1.0/public/api/v1/documentation/news
$currentPath = str_replace($config->site->url, '', $currentUrl); // Result: api/v1/documentation/news
if (strpos($currentUrl, 'public/index.php') !== false) {
    $redirect = $config->site->url; // In my case $config->site->url is https://localhost/framework-v1.0/
}

// 301 Redirect if /public/ - Replace only the first occurance of /public/ (Redirect to same url but without the first /public/ in it)
if (substr($currentPath, 0, 6) == 'public') {
    $needle = '/public/';
    $redirect = substr_replace($currentUrl, '/', strpos($currentUrl, $needle), strlen($needle));
    // Result with correct url: https://localhost/framework-v1.0/api/v1/documentation/news
}

if ($redirect) {
    return $di->getResponse()->redirect($redirect, true, 301)->send();
}

PS1: I think you can do something with the server configuration, but since in most cases I don't have access to the server I can't afford to go this path.

PS2: If you find a better solution please update the post to help others.



79.0k
Accepted
answer
edited Nov '16

The most convenient and recommended soultion is to set your webserver root to target directly your /public directory. That way, you'll have only one route. With nginx: root /path/to/your/web/folder/yourapplication/public

edited Nov '16

Thanks @nikolaymihaylov for your feedback, your answer is quite good, unfortunatly I'm not a regex pro, so I have some troubles with this.

@stamster your way of solving this problem is for me the best choice

Best luck to you!

Mike