mod_rewrite rules to drop www prefix

Posted on 2006-05-08
Medium Priority
Last Modified: 2008-02-01
I'm trying to write some mod_rewrite rules to drop a www prefix from http://www.example.com and redirect to http://example.com

Using Apache 1.3 and mod_rewrite I have the following rule set for the root of the site.

rewritecond %{http_host} ^www\.example\.com [nc]
rewriterule ^(.*)$ http://example.com/$1 [r=301,L]

This works fine for the root of the site, however I have another set of rewrite directives in the /blog/ subfolder to handle redirects for a wordpress blog so I need to declare rewrite rules in there as well.

For the /blog/ subfolder the .htaccess is

rewritecond %{http_host} ^www\.example.com [nc]
rewriterule ^(.*)$ http://example.com/blog/$1 [r=301,L]

This rule works fine if someone vists http://www.example.com/blog/ and redirects to http://example.com/blog/
However, if someone visits and leaves out the trailing slash, http://www.example.com/blog then the redirect goes funny and appends the local directory structure of the site so the URL looks like:

Any ideas where I'm going wrong?
Question by:salted
  • 2
LVL 10

Expert Comment

ID: 16633475
I know this isn't quite the question you are asking, but the easy way to accomplish the hostname change is to set:

ServerName example.com
UseCanonicalName On

(and get rid of the rewrite rules)

Author Comment

ID: 16634601
I'm on a shared hosting server so I can't access the httpd.conf to set that. Unless of course it is possible to set that in .htaccess?
LVL 18

Accepted Solution

Sam Panwar earned 1500 total points
ID: 16773826
LVL 18

Expert Comment

by:Sam Panwar
ID: 16947986
Hi periwinkle -

WE also not receive any response from the salted  so we can't assume what is going their so please author comment necessory for everyone to final disposition and may be his problem solved.

So we are waiting for author comment.

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month13 days, 19 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question