mod_rewrite to change default root directory

Discussion in 'Apache' started by mattbadley, Nov 10, 2010.

  1. #1
    Hi all,

    I'm tearing my hair out trying to fix this...

    I have a site mydomain.com and would like the default directory to be mydomain.com/site/

    My site uses an index.php file to display the correct page. Here's a snippet -

    
    <?php 
    	$url = $_SERVER['REQUEST_URI']; 
    	$urlend = substr($url, -1);
    	if ($urlend != "/") $url .= "/";
    	 
    	switch ($url) 
    	{
    	case '/contact/':
    			include_once "contact.html";
    			break;
    	}
    ?>
    
    Code (markup):
    ... so this would make the URL appear as mydomain.com/contact/ rather than mydoamin.com/contact.html

    This isn't working, I've had it working on another server and now I'm moving stuff to a new one and it's all breaking.

    I've created a .htaccess file in the root and this is what it says -

    
    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^mydomain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
    RewriteCond %{REQUEST_URI} !^/site/
    RewriteRule (.*) /site/$1
    </IfModule>
    
    Code (markup):
    This actually works for the index page and loads it as normal along with images, stylesheets etc... but no other pages work )it says the page can't be found.

    If I change the .htaccess to this -

    
    DirectoryIndex site/index.html
    <IfModule mod_rewrite.c>
    Options +FollowSymLinks
    Options -MultiViews
    RewriteEngine On	
    RewriteBase /site/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /site/index.php [L]
    </IfModule>
    
    Code (markup):
    ... then all the pages load correctly (with the right URL) but the stylesheets and images aren't working.

    The paths to all assets start with a /
    So for example the path to CSS is /css/screen.css

    If anyone could help me solve this I would greatly appreciate it! It seems I need a combination of the 2 .htaccess files to get this working correctly.
     
    mattbadley, Nov 10, 2010 IP