Forum Moderators: phranque
I have two domain names that I am directing to my own server. Using htaccess, I managed to direct these different domain names to their own directories within the document root.
So when a user types in...
[site1.com...]
On the server, it rewrites to the local file...
/var/www/vhosts/site1.com/index.php
where /var/www is the actual document root and the folder for the specific site is .../site1.com/...
In my htaccess file, I have...
Options +FollowSymlinks
DirectoryIndex index.php index.html
RewriteEngine on
RewriteCond %{HTTP_HOST} site1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/vhosts/site1.com/.*$
RewriteRule ^(.*)$ /vhosts/site1.com/$1 [L]
RewriteCond %{HTTP_HOST} site2.com$ [NC]
RewriteCond %{REQUEST_URI} !^/vhosts/site2.com/.*$
RewriteRule ^(.*)$ /vhosts/site2.com/$1 [L]
This works fine, however, I also want to make it so the user is not required to type the .php extension for PHP pages, so when htaccess encounters a url without an extension (and that is not a valid directory), it will rewrite the url to add the .php extension automatically and display the appropriate page to the user.
For this, I know the following code works when it is by itself...
Options +FollowSymlinks
DirectoryIndex index.php index.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This works fine when I am not directing my two domains to different directories, and herein lies the problem.
I need to somehow combine the two pieces of code to make it so the two different domains are still directed to their own separate directories, while in addition, the .php extension is not required in the url for PHP pages, for both websites.
I have tried several different combinations to do this, but I can't get it to work. I thought about just redirecting all my pages to a PHP script, where I know how to do everything from there, but I would like to stick with htaccess if at all possible, to save resources.
Any help would be greatly appreciated...
I didn't really think about it before, but this way, I can give each website its own htaccess file, therefore allowing me to just use that second bit of code for each site to strip the .php extensions. This also eliminates a lot of confusion since the two websites have two completely separate document roots now.
For anyone who's wondering how I did it, I just put this in my /etc/apache2/httpd.conf file (I'm using Debian Etch Linux)...
NameVirtualHost *
<VirtualHost *>
ServerAdmin admin@site1.com
DocumentRoot /var/www2/site1.com/
ServerName www.site1.com
ServerAlias site1.com
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/www2/docs/site1.com/access.log combined
ErrorLog /var/www2/docs/site1.com/error.log
<Directory "/var/www2/site1.com/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www2/docs/site1.com/secret_docs/">
AllowOverride AuthConfig
</Directory>
<Directory "/var/www2/docs/site1.com/more_secret_docs/">
AllowOverride AuthConfig
</Directory>
</VirtualHost>
<VirtualHost *>
ServerAdmin admin@site2.com
DocumentRoot /var/www2/site2.com/
ServerName www.site2.com
ServerAlias site2.com
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
CustomLog /var/www2/docs/site2.com/access.log combined
ErrorLog /var/www2/docs/site2.com/error.log
<Directory "/var/www2/site2.com/">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/var/www2/docs/site2.com/secret_docs/">
AllowOverride AuthConfig
</Directory>
<Directory "/var/www2/docs/site2.com/more_secret_docs/">
AllowOverride AuthConfig
</Directory>
</VirtualHost>
Note that "AllowOverride All" enables htaccess in the document root.
Then restart Apache...
/etc/init.d/apache2 restart
Hopefully that helps someone...