2

I have installed Craft successfully on a local Mac environment using Mamp. I can access the admin area and the default homepage when I go to this address localhost:8888/craft/public/ but I cannot access the site when I go to this address localhost:8888/craft/ Also, when I click on the news link on the default homepage it is a broken link unless I include the public in the URL.

localhost:8888/craft/news/2015/we-just-installed-craft

localhost:8888/craft/public/news/2015/we-just-installed-craft

How do I get around the public folder dilemma? Thanks in advance.

Luke Pearce
  • 3,863
  • 1
  • 11
  • 25
bsmity
  • 21
  • 3

2 Answers2

4

I'm on a Windows 10 environment with WAMP installed and I had exactly the same problem. Front End failed. Back End worked fine. News links failed. Clicking on website name link from the Localhost screen reveals directory contents. No matter how many times I kept clean installing Craft and tweaking and adding .htaccess files, the problem remained.

I solved the problem in the following way, without the need to add any .htaccess files. It's best to do a complete clean install of Craft.

You'll need the original, untouched, craft and public folders. Apologies if these following points go over what you already know.

  1. Copy the craft and public folders into the wamp/www/sitename folder as normal (or the Mac equivalent)
  2. Remove all the files from the public folder and place them at the base level (i.e. same level as the craft folder)
  3. Delete the public folder.
  4. Rename the root htaccess file to .htaccess
  5. Open up the .htaccess file and add this line

    RewriteBase /

    after this line

    RewriteEngine On

  6. In the root index.php file change line 4 to $craftPath = './craft';

  7. Create your localhost database.
  8. Set up your database connection details within craft/config/db.php.
  9. Via your browser, enter localhost/sitename/index.php/admin/install
  10. Click on the Begin button.
  11. Create your account page, fill out as normal.
  12. When you get to the Setup your site page, complete the Site URL details like this:

http://localhost/sitename

Finish off the install as normal and when your dashboard gets displayed, click on the Website name (top left) to go to the Front End and it will work. Click on the News link and it will work. As will Live Preview. Go to the Localhost page, click on the website name and you will go to your home page instead of having directory details displayed. Finally, if you upload this whole website to your live site it will still work.

Hope this helps someone.

As a slight caveat, I have only been testing Craft for a couple of days and installing it is about as far as I have got to at the moment. So if I've broken Craft no-no's like 'never delete the public folder, then I apologise (obviously, it doesn't have to be deleted and just remain empty)

  • This is a good answer, I've used it a few times, and I'm pretty sure this was the recommended install method for Craft 2, but, yes, the Craft community will call you mean names if you move the public files into your equivalent public directory. – David Rhoden Sep 07 '19 at 16:02
2

The web root needs to be set to the public/ folder. All of the other folders can reside outside of the webroot.

Or, you could add a .htaccess file to the craft/ folder with this in it

RewriteEngine on
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_URI}      !^/%{ENV:BASE}public/
RewriteCond %{REQUEST_URI}      !^%{ENV:BASE}public/
RewriteRule ^(.*)$              %{ENV:BASE}public/$1 [L]

And a .htaccess file to the public folder with...

RewriteEngine on
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=BASE:%1]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:BASE}index.php [L]

And that should let you access the website via just http://localhost:8888/craft/. I do this with my local dev sites but I rename the craft folder to match the domain.

Rick
  • 566
  • 2
  • 6
  • Note that /public/ still shows up while in the control panel, but shouldn't on the frontend of the site. And of course drop back to using a normal .htaccess file when deploying. – Rick Sep 09 '15 at 04:50
  • Thanks Rick. I did not have the public folder as the root and once I changed this it resolved itself. – bsmity Sep 10 '15 at 00:50