LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 06-03-2010, 10:23 AM   #1
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Rep: Reputation: 15
Apache2 running on SLES 11 mod_rewrite


I am running Apache2 on Novell SLES 11. I am using Virtual Hosting for two sites. One site only has a single domain whereas the other site has two domains registered. My question is surrounding my one virtual host with the two domains. I want anyone that point to mysite2.com gets redirected to mysite1.com. I am trying to do this for trending but not 100% on how to go about doing this. I've read about mod_rewrite but most of the information I've read about is about a single site running on Apache and not much around Virtual Hosting within Apache. Any suggestions? I am currently using Server Alias within my vhost.conf file which allows either mysite1.com or mysite2.com to see the site but I really want to redirect mysite2.com to mysite1.com.

Thanks in advance.
 
Old 06-03-2010, 11:43 AM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
Setup a separate virtual host and put something like this in the .htaccess for it--

Redirect Permanent / "http://domain1.com/"

or alternately you could add a similar line to the vhost for it.

You might be able to use a .htaccess file with something like...
RewriteCond %{HTTP_HOST} *domain2.tld*
RewriteRule ^/$ http://domain1.tld/ [L]

and then send that back to first domain, but I'd have to test syntax on that as I'm not sure off top of my head... typically a perm redirect is good enough.
 
Old 06-18-2010, 02:32 PM   #3
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
Alright. I have the rewrite stuff working but now am trying to do something else. Currently, the site or URL doesn't include the www. but just sitename.com so I would like to have it always redirect to www.site.com.

Here is my current .conf file and I am using virtual hosting.

<VirtualHost *:80>
ServerName site
ServerAlias site.com site1.com
ServerAlias www.site1.com www.site.com
DocumentRoot /srv/www/htdocs.site

RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*site1\.com\.?(:[0-9]*)?$ [NC]
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*site\.com\.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

I thought about adding this but then the site goes into some wierd loop and the site doesn't show up.
Code:
    RewriteCond %{HTTP_HOST} ^site\.com
    RewriteRule ^(.*)$ http://www.site.com/$1 [R=permanent,L]
ErrorLog /var/log/apache2/site-error_log
CustomLog /var/log/apache2/site-access_log combined

<Directory "/srv/www/htdocs.site">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>

In the end I want site.com or site1.com to redirect to www.site.com. Does this make sense?

Last edited by bkone; 06-18-2010 at 02:50 PM.
 
Old 06-18-2010, 03:30 PM   #4
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

You can use:
Code:
RewriteCond %{HTTP_HOST} !^www\.site\.com
RewriteRule (.*) http://www.site.com/$1 [R=permanent,L]
 
Old 06-21-2010, 07:26 AM   #5
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
When I use that I get this:

This webpage has a redirect loop.

The webpage at http://www.site.com/ has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.

I already have one rewrite rule so when some points their browser to site1.com it redirects to site.com. There are currently two domains going to this site, site.com and site1.com. I want either of these to goto www.site.com. So if I type http://site.com it redirect to http://www.site.com and if I type http://site1.com it redirects to http://www.site.com.

My original rewrite may not be needed but I am not too fluent with rewrite rules.
 
Old 06-21-2010, 07:59 AM   #6
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Hi,

Sure you don't need your rewrite rules.
And since site1.com is an alias to site.com you can simplify the condition to:
Code:
RewriteCond %{HTTP_HOST} !^www
Regards
 
Old 06-21-2010, 11:05 AM   #7
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
That didn't seem to work. I also noticed that the site1.com redirect to site.com isn't working all the time either. Surely, there must be an easy way to force site1.com and site.com to all direct to www.site.com.


<VirtualHost *:80>
ServerName site
ServerAlias site.com site1.com
ServerAlias www.site1.com www.site.com
DocumentRoot /srv/www/htdocs.site

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*site1\.com\.?(:[0-9]*)?$ [NC]
RewriteCond %{HTTP_HOST} ^([^.:]+\.)*site\.com\.?(:[0-9]*)?$ [NC]
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]


ErrorLog /var/log/apache2/site-error_log
CustomLog /var/log/apache2/site-access_log combined

<Directory "/srv/www/htdocs.site">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
 
Old 06-21-2010, 12:14 PM   #8
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
I told you that you don't need your rules. Besides your rules are wrong.
Rewriting is not working because all 3 conditions must be met.
Just use the following:
Code:
<VirtualHost *:80>
ServerName site
ServerAlias site.com site1.com
ServerAlias www.site1.com www.site.com
DocumentRoot /srv/www/htdocs.site

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www
RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]

ErrorLog /var/log/apache2/site-error_log
CustomLog /var/log/apache2/site-access_log combined

<Directory "/srv/www/htdocs.site">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
 
Old 06-21-2010, 01:04 PM   #9
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
Gotcha. However, I still have a problem. When I use site1.com it does add the www.site1.com and everything looks OK. But when I try site.com it tries to add www.site.com but I get the following in Firefox:

Resource is no longer available!

The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page.

If you think this is a server error, please contact the webmaster.
Error 403
www.site.com
Mon Jun 21 14:04:27 2010
Apache

I also noticed that site1.com doesn't redirect to www.site.com or even site.com. I basically am trying to bring in all traffic to www.site.com.
 
Old 06-21-2010, 01:21 PM   #10
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
Did you clear your browser's cache? It should work in all cases, except when you use www.site1.com (because the host starts with www), so I changed the condition accordingly.
You can also change the ServerName and ServerAlias and ditch the [R] flag:
Code:
<VirtualHost *:80>
ServerName www.site.com
ServerAlias site.com site1.com www.site1.com
DocumentRoot /srv/www/htdocs.site

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.site.com
RewriteRule ^(.*)$ http://www.site.com/$1 [L]

ErrorLog /var/log/apache2/site-error_log
CustomLog /var/log/apache2/site-access_log combined

<Directory "/srv/www/htdocs.site">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
 
Old 06-21-2010, 02:00 PM   #11
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
It looks like that would work but I must have something squirrelly cause I still get the message:

Resource is no longer available!

The requested URL is no longer available on this server and there is no forwarding address. If you followed a link from a foreign page, please contact the author of this page.

If you think this is a server error, please contact the webmaster.
Error 403
www.site.com
Mon Jun 21 15:00:36 2010
Apache

Let me check some logs. Any ideas? It is almost like the Apache doesn't know what to do with www.site.com but yet site.com works just fine. Everything looks good with DNS as well.
 
Old 06-21-2010, 02:09 PM   #12
bathory
LQ Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 13,163
Blog Entries: 1

Rep: Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032Reputation: 2032
The error is because you've used the R=301 (Moved Permanently) flag. That's why you should clear your browser's cache, or try to use another browser.
What happens if you visit http://www.site.com direclty?
 
Old 06-21-2010, 03:08 PM   #13
bkone
Member
 
Registered: Jun 2006
Distribution: SUSE, Red Hat, Oracle Linux, CentOS
Posts: 108

Original Poster
Rep: Reputation: 15
Smile

bathory, you are a genius! Thanks for your patience and assistance. All is working nicely now.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Apache2 mod_rewrite isn't working vargadanis Linux - Server 3 11-09-2009 08:21 PM
Apache2 how install mod_rewrite ? stefane321 Linux - Server 6 09-23-2007 05:29 PM
gallery2 and mod_rewrite in Apache2 swmok Linux - Software 1 07-08-2007 11:42 PM
apache2 recompile with mod_rewrite -- without losing any of the current config! scabrous1 Linux - Software 1 07-28-2006 04:56 PM
how to add mod_rewrite to apache2 scabrous1 Linux - Software 1 07-07-2006 09:48 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration