The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 18, 2019, 07:56:23 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: htaccess remove www when you dont know the domain name  (Read 4002 times)
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« on: April 08, 2008, 11:03:26 AM »

So im sure everyone has seen this cunk of htaccess code.
Code:
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]

how do I do this if I dont know what the domain is???
this does not work for obvious reasons if you understand mod-rewrite.
Code:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

I know if have seen it somewhere, i just cant seem to find it.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #1 on: April 08, 2008, 12:19:38 PM »

"This does not work for obvious reasons if you understand mod_rewrite..." lost me there man - I think you're on the right track.

The deal is that by time you're at the .htaccess level, the virtual host has already been captured by Apache - meaning that it was already defined (ergo you'd know what it was...) - if you're asking about a true wildcard capture, then you'll need to code that into your .httpd.conf file, where you can get ahead of the dispatch portion of the web request... unless your httpd.conf file IS already wildcarded and the virtual host is pointing to (this) directory...

I don't have time to check, but I think you're really close... I have stuff that is similar (albeit for a completely different purpose) in some of my httpd.conf files... but the truth is that there are some differences in behavior between httpd.conf and htaccess, so I could be leading you astray. The key is to collect and use a backreference from the condition, not the rule...

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

...but if that pees the bed on you then do it right in PHP... (assuming you have all requests going to a dynamic handler...)

(Didn't check this, you may need a / in the Rule between the % and $ reference)
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: April 08, 2008, 03:15:12 PM »

duh, i think you pointed me in the right direction. I dont have a chance to check yet, but will post when i get it sorted.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: April 08, 2008, 05:32:40 PM »

lol. actually that was the answer. thanks perk
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: April 08, 2008, 05:40:38 PM »

nw man... stumped me for a moment when you first posted and couldn't let it go LOL
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #5 on: April 08, 2008, 05:51:33 PM »

bah it needs a slash.

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

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!