Link to home
Start Free TrialLog in
Avatar of salted
salted

asked on

mod_rewrite rules to drop www prefix

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:
http://example.com/blog//home/users/xyz/domains/example.com/public_html/blog

Any ideas where I'm going wrong?
Avatar of sleep_furiously
sleep_furiously

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)
Avatar of salted

ASKER

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?
ASKER CERTIFIED SOLUTION
Avatar of Sam Panwar
Sam Panwar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.