Apache - Can no longer use mod_rewrite after adding virtual directory - apache

My apache server is set up with a very basic configuration. I used to serve just one website from apache, let's call it example.com. Within the httpd.conf file, I had some code to force the website to always show www in the url.
I recently added a subdomain for the site, blog.example.com. In order to do this I needed to create 2 virtual directory directives within my httpd.conf file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerName blog.example.com
DocumentRoot /var/www/blog
</VirtualHost>
Immediately after this, I kept my rewrite code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
The problem is that this after adding the Virtual Directory code, the rewrite code is no longer working. I tried creating the following Directory directive, which doesn't seem to work at all.
<Directory "/var/www/html">
Options Indexes MultiViews FollowSymLinks
Order Deny,Allow
Allow from all
AllowOverride All
</Directory>
Additionally, I tried creating a .htaccess file in the html folder of the website and restarting apache, but nothing that I put in there is getting noticed at all.
Any help is greatly appreciated. Thanks!

Answer from comment by original poster:
Ok, I figured it out. I just needed to move the rewrite logic within the start and end Directives. Thanks for the help #animuson. – Henry Wrinkler

Related

Get 403 error when create multiple vhost outside the XAMMP htdocs

I know this is quite old problem, but i have no luck when trying to find the right solution. I have tried every possible configuration but still no luck. I'm working on PHP project using laravel. I got no problem at all when set up the laravel.labs as vhost INSIDE the htdocs. But when I move it into separated folder which is C:\vhosts i got this 403 error. The purpose of vhosts folder is to hold multiple vhost. The following are my configuration:
System:
windows 7 64-bit
XAMPP v 1.7.7
Apache/2.2.21
PHP: 5.3.8
httpd.conf :
# Virtual hosts
Include "conf/extra/httpd-vhosts.conf"
httpd-vhosts.conf
NameVirtualHost *:80
<VirtualHost *:80>
DocumentRoot C:/xampp/htdocs
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot c:/vhosts/laravel_labs/public
ServerName laravel.labs
</VirtualHost>
c:\windows\system32\driver\etc\host
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 laravel.labs
.htaccess (inside the C:\vhosts\laravel_labs\public)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can anyone help to find something that i missed? any advice guys? thanks
Every time you add a new Alias or DocumentRoot, you need a <directory> section just like the one for your default DocumentRoot. Depending on your release, this either has something like
Order deny,allow
or
require all granted
Finally i found The answer for this problem. I have to add some lines in httpd.conf
Something like this:
Alias /laravel_labs/public "C:/vhosts/laravel_labs/public/"
<Directory "C:/vhosts/laravel_labs/public/">
AllowOverride All
Order Allow,deny
Allow from all
</Directory>
See the detail explanation here :
http://www.youtube.com/watch?v=Z250saioXIs

Apache Settings For Unique subdomain and Domain Directories

I'm fairly capable with most of the web server configuration but I have run into a problem.
I have a VPS install with one sub domain. Currently the link sub1.domain.com points to the root of the public_html/www folder.
I am now looking at creating new sub domains as well as using domain.com to point to a wordpress installation.
From my understanding, I must edit the apache httpd.conf file, but I am unsure exactly how to do this. I would expect that I would have to have domain.com point to a subdirectory as well as sub2.domain.com point to a different sub directory. Something like the following.
sub1.domain.com >> rootPathForApache/
domain.com >> rootPathForApache/domain.com/
sub2domain.com >> rootPathForApache/sub2/
Could somebody please point me in the right direction.
Kind Regards,
You have a couple options:
Option 1: Apache's <VirtualHost> directive could solve this (multiple). This is only available in the server config context.
<VirtualHost *:80>
DocumentRoot "rootPathForApache/"
ServerName sub1.domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "rootPathForApache/domain.com/"
ServerName domain.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "rootPathForApache/sub2/"
ServerName sub2domain.com
</VirtualHost>
Option 2: Use a .htaccess file in rootPathForApache with the mod_rewrite module loaded, and introduce ServerAlias directive into either your existing <VirtualHost> directive.
.htaccess contents (or if you want within the VirtualHost directive itself):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1.domain.com
RewriteRule ^(.*) $1 [L]
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*) domain.com/$1 [L]
RewriteCond %{HTTP_HOST} ^sub2domain.com
RewriteRule ^(.*) sub2/$1 [L]
...and the ServerAlias:
ServerName domain.com
ServerAlias sub1.domain.com sub2domain.com
The latter solution (option 2) just seems ridiculous, especially since ServerAlias is only available within a VirtualHost directive and mod_rewritehtaccess files add unnecessary overhead. That and it doesn't seem natural. I guess it illustrates the flexibility of Apache's configuration and modules.

apache vhosts ignores rewriterule

I have the following vhosts file:
directoryindex .index.php index.html index.php
options -indexes -multiviews +followsymlinks
<directory /Volumes/dev1/http>
allowoverride all
</directory>
<virtualhost *:80>
documentroot "/Volumes/dev1/http"
rewriteengine on
rewriterule ^.*$ /msg.php [l]
</virtualhost>
There are other virtual hosts, but the one from above is the "catch-all". The problem is that the rewriterule is totally ignored.
Going to site.com gives a 403 error and site.com/test just returns a 404 Not Found page instead of serving the msg.php file.
The server is a OS X 10.8 Server running Apache 2.2.24
it seems that you are in the wrong dir.
For what reason ever Apple left the .conf files for the virtual host under this directory.
/Library/Server/Web/Config/apache2/sites

keep name domain htaccess

I have a little problem. I have a hosting with piensasolutions and two domains. The first domain controls the hosting and the second use a domain parking.
Let a.com be the first domain and b.com be the second domain.
My domain parking for b.com allows me to redirect from b.com to a.com/folder, (where folder contains the web page where I want to point with b.com).
The problem is that I want to show b.com URL instead of a.com/folder URL, I am not sure if the problem is from piensasolutions because I tried several .htaccess files but I was not able to change the URL of the web browser. This was my last test which supposes to keep the original URL.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?a.com/folder/$
RewriteRule ^(.*)$ /a.com/folder/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?b.com$
RewriteRule ^(.*)$ /b.com/$1 [L]
For additional information, I put the .htaccess file inside the a.com/folder. I am not sure about the redirection of piensasolutions.
If your are able to help me, I will be really grateful, and also if you have dedicated a couple of minutes even if you have not find the anwser.
Thank you!
I think that the best way is usig sites-avaiable.
First of all,
$ vi /etc/host
and copy the next line:
127.0.0.1 a
127.0.0.1 b
Then, create two new sites:
$ touch /etc/apache2/sites-avaiable/a
$ touch /etc/apache2/sites-avaiable/b
and edit them
$ vi /etc/apache2/sites-avaiables/a
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName a
DocumentRoot /your_path_to_server/a/folder
<Directory /your_path_to_server/a/folder/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and
$ vi /etc/apache2/sites-avaiables/b
<VirtualHost *:80>
ServerAdmin webmaster#localhost
ServerName b
DocumentRoot /your_path_to_server/b
<Directory /your_path_to_server/b/>
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
and that's all. This should work.

File not found with VirtualHost and mod_rewrite

I'm bulding a RESTful api based on Tonic.
On my developer machine and our stage server we use virtual hosts.
Tonic uses a .htaccess file to translate the incomming calls to it's dispatcher.php file. This works fine on servers without VirtualHosts enabled.
However if i enable VirtualHosts i get a file not found even thought the path and name to the file is correct.
Here is the VirtualHost setup on my developer machine.
<VirtualHost *:80>
ServerAdmin admin#xxxxxxxxxxxx
ServerAlias *.dev.xxxxx
VirtualDocumentRoot /home/xxxxxxxx/workspace/%1
<Directory /home/xxxxxxxx/workspace/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And Tonic's .htacces located in a folder called rest in the project root:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !dispatch\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* dispatch.php [L,QSA]
</IfModule>
A call to http://project.dev.xxxxx/rest/ gives:
Not Found
The requested URL /home/xxxxxxxx/workspace/project/rest/dispatch.php was
not found on this server.
Apache/2.2.22 (Ubuntu) Server at xxxxxxx Port 80
It appears as though you're misusing VirtualDocumentRoot. Try changing it to:
DocumentRoot /home/xxxxxxxx/workspace/project/rest
Also, here's a good explanation on the VirtualDynamicRoot: Dynamically configured mass virtual hosting
Hope that helps.