Linux - ServerThis forum is for the discussion of Linux Software used in a server related context.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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>
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.
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>
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.
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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.