LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2013, 02:29 PM   #1
Pyplate
LQ Newbie
 
Registered: Jun 2013
Location: Worcestershire, UK
Distribution: Ubuntu,lubuntu,raspbian
Posts: 17
Blog Entries: 30

Rep: Reputation: Disabled
Changing my site's domain - need to set up an Apache virtual host for 301 redirects


I'm using a dynamic DNS service, and I want to switch from a free domain name to my own domain name. I'm not that experianced at working with Apache, so I'd appreciate it if I could get a sanity check. I'm currently using a single server connected to my home router. I'm running a Debian based Linux called Raspbian, and I'm using Apache2 with a home made CMS. My site is currently at http://myraspberrypi.no-ip.biz.

I plan to create two virtual host files in /etc/apaches/sites-available like this:

/etc/apache2/sites-available/mynewdomain.com

Code:
<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /var/www
	ServerName mynewdomain.com
	<Directory />
		Options FollowSymLinks
		AllowOverride All
	</Directory>
	<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                AddHandler cgi-script .py
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined

</VirtualHost>


/etc/apache2/sites-available/myolddomain.com

Code:
<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /var/www2
	ServerName myolddomain.com
	<Directory />
		Options FollowSymLinks
		AllowOverride All
	</Directory>
	<Directory /var/www2/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride All
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin2/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
                AddHandler cgi-script .py
		Order allow,deny
		Allow from all
	</Directory>

	#ErrorLog ${APACHE_LOG_DIR}/error.log
	ErrorLog ${APACHE_LOG_DIR}/error2.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	#CustomLog ${APACHE_LOG_DIR}/access.log combined
	CustomLog ${APACHE_LOG_DIR}/access2.log combined

</VirtualHost>
Then I'll execute these commands:

sudo a2ensite myolddomain.com
sudo a2ensite mynewdomain.com

In /var/www2, I'm going to duplicate the directory structure and fill it with htaccess files containing 301 redirects.

Before I try this, I want to make sure I know what I'm doing, so I'd appreciate it if someone could let me know if I'm on the right track. Is this approach likely to hurt my search rankings?
 
Old 07-10-2013, 09:28 PM   #2
Pyplate
LQ Newbie
 
Registered: Jun 2013
Location: Worcestershire, UK
Distribution: Ubuntu,lubuntu,raspbian
Posts: 17

Original Poster
Blog Entries: 30

Rep: Reputation: Disabled
Maybe I can put a redirect in an htaccess file?

I've been reading about this some more, and it looks like I may not need to add a virtual host. I could just add a redirect in the .htaccess file. Is this the correct syntax to use?

Code:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^olddomain.com$
RewriteRule ^(.*)$ http://newdomain.com/$1 [R=301]
 
Old 07-11-2013, 05:58 PM   #3
Pyplate
LQ Newbie
 
Registered: Jun 2013
Location: Worcestershire, UK
Distribution: Ubuntu,lubuntu,raspbian
Posts: 17

Original Poster
Blog Entries: 30

Rep: Reputation: Disabled
I've bought the new domain, but can't get the 301 redirect to work.

So I've added two lines to my .htaccess file under the comment about the 301 redirect:

Code:
# .htaccess
DirectoryIndex cgi-bin/pyindex.cgi
RewriteEngine on
# rewrite rules for 301 redirect
RewriteCond %{HTTP_HOST} ^myraspberrypi.no-ip.biz/$
RewriteRule ^(.*)$ http://raspberrywebserver.com/$1 [R=301]
# rules for CMS
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !favicon.ico
RewriteRule ^(.*)$ cgi-bin/pyindex.cgi?q=$1 [L,QSA]
My site is visible at the new domain, but if I go to the old domain, I can still see my site, but and the old domain is displayed in the browser's url. So it looks as if the redirect isn't working. I tried adding escape characters to the lines I added like this:

Code:
RewriteCond %{HTTP_HOST} ^myraspberrypi\.no-ip\.biz/$
RewriteRule ^(.*)$ http\:\/\/raspberrywebserver\.com/$1 [R=301]
Still no redirection. Any idea what I'm doing wrong?
 
Old 07-15-2013, 05:57 AM   #4
Pyplate
LQ Newbie
 
Registered: Jun 2013
Location: Worcestershire, UK
Distribution: Ubuntu,lubuntu,raspbian
Posts: 17

Original Poster
Blog Entries: 30

Rep: Reputation: Disabled
I got it working

I added a '*' to the end of the rewrite condition:

RewriteCond %{HTTP_HOST} ^myraspberrypi.no-ip.biz/*$

Now each page redirects to the equivalent page with the new domain name.

Last edited by Pyplate; 07-15-2013 at 05:57 AM. Reason: fixed typo
 
  


Reply

Tags
apache2, redirect, virtual host


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
how to set apache virtual hosts Host-Based Security? hocheetiong Linux - Newbie 2 01-17-2014 05:20 AM
[SOLVED] Creating 301 Permanent Redirect to a domain with same docroot as old domain. MarkToronto Linux - Newbie 1 05-15-2013 04:35 AM
[SOLVED] Query: CentOS 6.2 Apache 2.2.15 Virtual Host Set-up LouArnold Linux - Server 11 03-02-2012 07:10 PM
Apache site redirects using what rule? foo.site.com -> www.site.com/foo LaughingBoy Linux - Server 2 04-16-2009 10:51 PM
virtual host redirects with mod_rewrite sqn Linux - Software 1 10-11-2005 09:47 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 01:31 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