2

I'm developing a site at the moment and noticed when I uploaded it to my staging server, all my links show correctly in the address bar, but show the index template.

My folder structure is as follows

/
  /pub
    index.php
    .htaccess
  /system
  /templates
  .htaccess

My site runs inside /pub - Because I'm not able to specify with my host what the root directory of a site should be, they've advised that I use htaccess instead.

That .htaccess file sits in / and looks like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^stage.domain.co$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.stage.domain.co$
RewriteCond %{REQUEST_URI} !pub/
RewriteRule (.*) /pub/$1 [L]

I also have a .htaccess file in /pub/ which looks like this:

# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /

# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]

# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

I want to remove index.php, AND the pub directory so my site will serve up URLs such as stage.domain.com/exercises instead of stage.domain.com/pub/index.php/exercises

So two questions.. Should I be using two htaccess files like I am at the moment, one to redirect from /pub/, and one to remove index.php, or should they be in one file, and where should it be?

What needs to be in those files to make it work?! I've tried what EE suggested by removing the slash before index.php on the last line, and adding a question mark.

  • Are you sure that the /pub folder doesn't map to the root of your website - that's the norm. It would be really weird to have your site start example.com/pub. If so then you just need the standard .htaccess to removed index.php – Jon Rhoades Mar 11 '13 at 11:17
  • That's what I WANT to happen, but no. If I remove the first htaccess file, you can only access the site through domain.co/pub. If you go to domain.co, you get a directory listing the same as I put in my question. I had to put the first htaccess file in because my hosting company won't let me specify the root directory. – darylknight Mar 11 '13 at 11:22
  • Surely I can't be the only person who's host won't let them change their root directory? – darylknight Mar 13 '13 at 17:11
  • why not just move everything 1 level up? – Steven Grant Mar 15 '13 at 8:28
  • I can't add anything the the directory above my root. – darylknight Mar 15 '13 at 22:55
3

What you are doing doesn't make any sense. There is no point having a separate /pub directory if everything is still within the webroot.

The only reason you would have a separate pub (or public_html) directory is if you want to keep your system folder above the webroot. This adds an extra layer of security, though it only really protects against a misconfigured apache and isn't really worth the extra work on shared servers IMO.

Just move your index.php and .htaccess out of /pub, and stick to the convention:

/
  /system
  /templates
  .htaccess
  index.php

To remove /index.php/ from your URLs, just follow Removing index.php from URLs, so your htaccess should look like this:

RewriteEngine On

# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.