Simple musings of a Drupal developer.

On Rewrites and Redirects

By krs · December 17, 2008 · 1 Comment · 417 Views

Webserver configuration is not a task I have to do very often. A client was replacing an old site with a new one at the same URL, but they needed many of the old links to still work correctly, for SEO, bookmarks, and general usability purposes. To further complicate matters, there were actually 2 wordpress blogs hosted at the same domain, and the webserver needed to redirect some of their links, while leaving the wordpress blog itself untouched!

So with great trepidation, I requested the list of URLs to be handled from the client. What arrived was a seemingly random assortment of domains and URLs. All together, I used a number of different techniques to match them all. The next time this comes up, I'll probably have fogotten all the syntax, so I'll preserve everything I learned here. I found other examples to be extremely useful, so my hope is that others will benefit from these as well.

All of these configuration directives were in apache's httpd-vhosts.conf, but could also be used in a .htaccess file, or possibly httpd.conf. Probably other places too, knowing apache. It helps to know that Drupal's home directory was /home/web/sites/production/www, and that's what the virtual host was using as DocumentRoot.

First of all, how was a wordpress blog being hosted on the same domain as the Drupal site - but no files were in Drupal's home folder?

The Alias Directive

Alias /blogs/someblog/ /home/web/sites/production/other/blogs/someblog/

This lets apache know that any URL's coming in like /blogs/exampleblog/ are really hosted in a totally different document root.

The Redirect Directive

Redirect permanent /blogs/someblog/author/someauthor/index.php http://www.example.com/user/someauthor/profile-page

This lets apache know you like to send that specific URL to a new place. Aliases and Redirects are evaluated in the order they appear. Since this one is more specific than the alias above, it actually has to go first. This was we can redirect some links from the wordpress blog to our new Drupal site, without harming the rest of it.

The ServerName Directive

ServerName example.com

This is the domain name you want this virtual host to use.

The ServerAlias Directive

ServerAlias www.another.com subdomain.example.com www.example2.com

These are other aliases for the server name that the vhost should use. Note that these don't get converted to the ServerName, apache just treats them as equally valid. This can cause problems with Drupal's settings.php file - which is usually loaded for specific server names. You can make use of a sites/default/settings.php, but that is generally a practice I don't recommend. If you can, using RewriteRules may be a better option.

The RewriteCond and RewriteRule Directives

RewriteEngine On

Have to set this for the Rewrite statements to be evaluated at all.

RewriteCond %{HTTP_HOST} ^oldsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.newsite.com/blogs/fashion [L,R=301]

This sends all traffic from the old site (which just hosted a blog) to one particular URL on the new site (which hosts a similar blog). [NC] means its not case sensitive. [L] stands for Last, and tells apache to stop evaluating and go there, and [R=301] sends the browser an http status code 301, which should help search engines find your new content. So oldsite.com/articles/my-article becomes www.newsite.com/blogs/fashion.

RewriteCond %{HTTP_HOST} ^(www\.)?oldersite\.(com|net|info)$ [NC]
RewriteRule ^(.*)$ http://www.newsite.com$1 [L,R=301]

This sends traffic from the old domains to the new site as wel, but it keeps the rest of the URL intact. The difference comes form the addition of the $1 at the end of the new domain. So www.oldersite.com/articles/my-article becomes www.newsite.com/articles/my-article.

posted by Unforgettable Name
Fri, 09/25/2009 - 6:04pm

hi krs. setting up servers is one thing that I really could do without. That's for sure. Some cool stuff you did there.


Post New Comment

If you are already an OnSugar member, or would like to receive email alerts as new comments are made, please login or register for OnSugar. Or connect with your Facebook account: .
The content of this field is kept private and will not be shown publicly.

About Me

Drupal.org username: kscheirer

subscribe

Drupal Cred

Drupal Camp Los Angeles 2009 - August 8-9th

Archive

August 2009
SMTWTFS
 1
2345678
9101112131415
16171819202122
23242526272829
3031 
July 2009
SMTWTFS
 1234
567891011
12131415161718
19202122232425
262728293031