Forum Moderators: phranque

Message Too Old, No Replies

rewrite to canonicalize the subdomain

         

httpwebwitch

4:24 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need the following to happen

if someone requests "xyz.example.com/blog/", redirect to "www.example.com/blog/"
if someone requests "abc.example.com/blog/", redirect to "www.example.com/blog/"
if someone requests "123.example.com/blog/", redirect to "www.example.com/blog/"

... but only in the "blog" directory. everywhere else but the blog, those other subdomains are legit.

sounds simple. So why am I struggling with this?

this is what I have

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^blog/(.*)$ http://www.example.com/blog/$1 [R=301]

what'm I doing wrong

g1smd

11:26 am on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is your DNS set up to resolve the other subdomains to your server?

Don't forget to add the [L] flag to your [R=301] flag.

jdMorgan

1:15 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And, is your server set up to resolve requests for those subdomains to the same subdirectory in which this code resides? If you used a control panel to declare them as "add-on domains," the answer may be "no," in which case, your code won't be invoked for subdomain requests.

Jim

httpwebwitch

1:49 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



yes and yes - all my subdomains are pointing to the same public_html folder, where my code shows slightly different content based on the subdomain


I've used subdomains to offer international translations. I serve the same public_html folder whether I'm at es.example.com (Spanish) or fr.example.com (French) or www.example.com (English), and the code delivers a different i18n version of the content.


but the /blog is only in english. So I wanna force a 301 redirect to canonicalize it at the www subdomain

I tried this:


RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^blog/(.*)$ http://www.example.com/blog/$1 [R=301,L]


but it's not doing what I intend - I can still access fr.example.com/blog/, it shows the blog - but not in french - and it doesn't redirect to www.example.com/blog/

g1smd

3:26 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Did you flush your browser cache before retesting?

What does the Live HTTP Headers extension report for those URLs?

httpwebwitch

7:45 pm on Sep 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



browser cache flushed

HTTP header is a plain 200 OK, showing the english content but with the fr.example.com host. Same as if I didn't have those lines in my .htaccess at all.

There's got to be something wrong with the regex pattern or the RewriteRule, or something.

jdMorgan

2:26 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Disable MultiViews (content negotiation) and AcceptPathInfo (Apache 2.x only) and see if that helps.

Otherwise, you've got some other code interfering with this, and the requests are not being delivered to your .htaccess file, so the rule isn't executing -- perhaps an Alias or ScriptAlias setting in a server config file.

Basically, your rewriterule is fine, so it's a matter of something else interfering.

Jim

httpwebwitch

4:56 pm on Sep 30, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It must be WordPress, $#@!

jdMorgan

1:19 am on Oct 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The redirect code must precede the WP rewrite code. This should probably be the second-to-last external redirect rule, followed only by your more generic hostname-only canonicalization rule, and the WP code should be the final internal rewrite rule, because both the last redirect and the last rewrite are "catch-all" rules (as regards the URL-path).

Jim

httpwebwitch

1:41 pm on Oct 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



that was it. I was putting these rules in .htaccess in the root folder (public_html/.htaccess), while in the /blog subfolder there was another .htaccess with the Wordpress rewrite rules in it.

I removed them from the root .htaccess, and added this at the very top of public_html/blog/.htaccess

RewriteEngine On
# the blog section is only in www
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/blog/$1 [R=301,L]


Now it all works as intended.

Thanks!