mod_rewrite rules to drop www prefix

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

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 ^(.*)$$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\ [nc]
rewriterule ^(.*)$$1 [r=301,L]

This rule works fine if someone vists and redirects to
However, if someone visits and leaves out the trailing slash, 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
    LVL 10

    Expert Comment

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

    UseCanonicalName On

    (and get rid of the rewrite rules)

    Author Comment

    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

    LVL 18

    Expert Comment

    by:Sam Panwar
    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

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    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…
    Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    745 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

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now