php.htaccessmod-rewritewampmod-vhost-alias

wamp local subdomains and htaccess rewrite rules


i recently fixed my site up with a bit up php mvc magic for which i use those settings in my .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]

it worked like a charm. the only problem i encountered was that when i reference files like css or images relatively, the browser assumed that we moved down the folder structure due to the dashes in the url localhost/projectname/controllername/action/id

so the css/styles.css was requested at localhost/controllername/action/id/css/styles.css having absolute paths fixed that problem but didn´t work well with having a localtest server and a remote live and development server after there was no "projectname"- folder

i decided to configure my local wamp server so i can access my different project as a subdomain to my localhost like "http://projectname.localhost".

i activated the relevant apache modules, edited my hosts file and changed my httpd-vhosts.conf to

NameVirtualHost *:80
<VirtualHost *:80>
    ServerName localhost.com
    ServerAlias www.localhost.com
    DocumentRoot "C:\wamp\www"
    ErrorLog "logs\errors.log"
    <directory "C:\wamp\www">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from all
    </directory>
</VirtualHost>

<VirtualHost *:80>
    ServerName localhost.com
    ServerAlias *.localhost.com
    VirtualDocumentRoot "C:\wamp\www\subdomains\%1"
    ErrorLog "logs\errors.log"
    <directory "C:\wamp\www\subdomains\%1">
        Options Indexes FollowSymLinks
        AllowOverride all
        Order Deny,Allow
        Deny from all
        Allow from all
    </directory>
</VirtualHost>

now when i call projectname.localhost.com/controllername/action/id it tells me The requested URL /subdomains/projectname/index.php was not found on this server. while projectname.localhost.com/index.php still supplies me with the right file.

i have heard that there might be a problem with the $_SERVER['DOCUMENT_ROOT'];. that doesn't get set properly but knowing that has not helped me fix that problem yet.

can someone point me into the right direction or suggest another environment configuration that makes testing under those circumstances possible?


Solution

  • changing my .htaccess to

    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^([a-zA-Z0-9]*)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?/?$ index.php?controller=$1&action=$2&arg=$3 [NC,L]
    

    solved that issue. the magic line is RewriteBase /

    seems like the server was really looking for the index.php in the wrong place.

    hope this helps other people having the same problem... :)