LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   .htaccess and redirect - not making sense (https://www.linuxquestions.org/questions/linux-software-2/htaccess-and-redirect-not-making-sense-4175450261/)

compused 02-15-2013 08:05 AM

.htaccess and redirect - not making sense
 
Hi
I have these two virtualhost entries in httpd.conf; here is the first:
Code:

NameVirtualHost *:80
#
<VirtualHost *:80>
DocumentRoot "/home/main_directory/drupal"
ServerName hol-server.com.au
<Directory "/home/main_directory/drupal">
          Order                          Allow,Deny
          Allow                          From All
        Options FollowSymlinks +Indexes
        AllowOverride All
</Directory>
</VirtualHost>

here is the second, which has nothing to do with drupal - the problem is that the rewrite rule is ignored (see the # lines):
Code:

<VirtualHost *:80>
DocumentRoot /var/www/html/cpserver
ServerName www.cpserver.org.au
<Directory "/var/www/html/cpserver">
AllowOverride none
Order Allow,Deny
Allow from all
Options FollowSymlinks +Indexes
<IfModule mod_rewrite.c>
 RewriteEngine on
 #RewriteBase /var/www/html/ # did not help
 #the expected rewrite is not happening
 RewriteCond %{HTTP_HOST} ^cpserver\.org\.au$ [NC]
 RewriteRule ^(.*)$ http://www.cpserver.org.au/$1 [L,R=301]
</IfModule>
</Directory>
</VirtualHost>

in /home/main_directory/drupal/.htaccess I have:
Code:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^hol-server\.com\.au$ [NC]
  RewriteRule ^(.*)$ http://www.hol-server.com.au/$1 [L,R=301]
# uncommenting the line below enables the rewrite, but it should not as cpserver is  under a completely different directory
#  RewriteCond %{HTTP_HOST} ^cpserver\.org\.au$ [NC]
#  RewriteRule ^(.*)$ http://www.cpserver.org.au/$1 [L,R=301]
</IfModule>

the other anomaly is that moving the www.cpserver.org.au virtualhost to above the hol-server.com.au entry in httpd.conf, enables the rewrite but then hol-server.com.au redirects to cpserver.org.au

apache is restarted after each change, though changes in .htaccess do not seem to need a restart.

Any help would be good - have been working on this for a few nights now.
Compfused

fortran 02-15-2013 08:22 AM

Are you using apache2 for web development environment?
I mean, you are not using XAMPP. correct?

1) Can you check following line in your /etc/apache2/apache2.conf
Code:

AddHandler application/x-httpd-php .htm
If it is not there then add it, Now restart apache and try.

2) Check following line in your apache conf file
Code:

AccessFileName .htaccess
3) Add following lines in your httpd.conf file
Code:

<Directory /your/drupal/project/path>
    AddHandler cgi-script .cgi
    Options +Indexes +ExecCGI
    AllowOverride all
    </Directory>

Restart Apache.

compused 02-15-2013 09:03 AM

thanks ++ PK - the AddHandler directive worked a treat!
So simple!
Compfused

compused 02-16-2013 09:13 AM

An update on this...its an apache2 linux server, and the expected rewrite is not happening after I moved another drupal based virtualhost, call it drupal2, to the new location of /var/www/html:

ie we have
hol-server.com.au with DirectoryRoot of /home/main_directory/drupal
and
drup2-server.com.au with DirectoryRoot of /var/www/html/drupal2, but originally /home/main_directory/drupal2

For rewriting purposes, even after the move to the completely different directory, I can't seem to free drup2-server.com.au from its dependency on the original /home/main_directory/drupal/.htaccess file. The moved site is otherwise working normally


drup2-server.com.au 's httpd.conf entry is now:
Code:

<VirtualHost *:80>
DocumentRoot "/var/www/html/drupal2"
ServerName drup2-server.com.au
<Directory "/var/www/html/drupal2">
          Order                          Allow,Deny
          Allow                          From All
        Options FollowSymlinks +Indexes
        AllowOverride All  <---'All' is needed otherwise drupal2 doesn't work
<IfModule mod_rewrite.c>
 RewriteEngine on
 #RewriteBase /var/www/html/ <-- this did not help
 #the expected rewrite is not happening:
 # instead of the rewrite I get 'hol-server.com.au' when typing 'drup2-server.com.au',
 # using the 'www' prefix shows the drup2-server.com.au normally
 # the only way to rewrite the www is via an entry in /home/main_directory/drupal/.htaccess
 RewriteCond %{HTTP_HOST} ^drup2-server\.com\.au$ [NC]
 RewriteRule ^(.*)$ http://www.drup2-server.com.au/$1 [L,R=301]
</IfModule>
</Directory>
</VirtualHost>

Also in httpd.conf as advised above I have:
Code:

AccessFileName .htaccess
AddHandler application/x-httpd-php .htm


I also tried putting the rewrite directives into /var/www/html/drupal2/.htaccess, but that did not help either. Apache was restarted.
Any idea on this one please?
Thanks
Compused
Edit: have removed the [SOLVED] label due to this subsequent unexpected finding.

fortran 02-18-2013 12:30 AM

Add following lines in your httpd.conf file
Code:


<Directory /your/drupal/project/path>
    AddHandler cgi-script .cgi
    Options +Indexes +ExecCGI
    AllowOverride all
    </Directory>

Restart Apache.

d3vrandom 02-18-2013 02:03 AM

The problem is simple. You need to use serveralias if you want multiple names pointing to the same virtual host. So in the case of the first virtual host you need to add the www as the server alias:

Code:

servername hol-server.com.au
Serveralias www.hol-server.com.au  #add this

And in the case of the second one the non-www version:

Code:

Servername www.cpserver.org.au
Serveralias cpserver.org.au #add this


compused 02-18-2013 08:35 AM

Thanks, both of you guys. I'm crying. Its working. Fantastic relief.
Think it was the serveralias that did the trick.
Compfused


All times are GMT -5. The time now is 09:36 AM.