homepage Welcome to WebmasterWorld Guest from 54.167.10.244
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Apache Subdomain Redirect .htaccess Help
apache, redirect, subdomain, wildcard
celebbiz




msg:4521263
 9:36 pm on Nov 21, 2012 (gmt 0)
Hey Guys, I have been racking my brain on this one for a while and after days of trying/breaking stuff, I figured I come ask the pros here:

I have about 100 subdomains that are no longer on my dedicated Apache server, all that is listed is the main domain and I have turned on wildcard through records. The issue I am having is I am missing some code to make all those old non used subdomains when typed in or followed by an old link somewhere work properly and can't find it anywhere.

Right now no matter what sub.domain is typed in it forwards to mydomain.com and that is fine for sub.domains that never existed but I would like to say if this certain sub1.mydomain.com is hit that used to be a live link take it to the new page that would be something like mydomain.com/sub1-page-here/

Here is the code I have in the .htaccess file I think I am missing the code that actually makes these work there is about 100 of them:

##301 SUB.DOMAIN REDIRECTS##
RewriteCond %{HTTP_HOST} ^sub1\.mydomain\.com [NC]
RewriteRule (.*) http://domain.com/sub1-page-here/$1 [L,R=301]
#
RewriteCond %{HTTP_HOST} ^sub2\.mydomain\.com [NC]
RewriteRule (.*) http://domain.com/sub2-page-here/$1 [L,R=301]
#
etc. etc.

Your help will be very much appreciated ;[smilestopper])

 

lucy24




msg:4521295
 10:46 pm on Nov 21, 2012 (gmt 0)

Did you remember

RewriteEngine On

?

100 separate redirects is an awful lot. Can't you fit them into a pattern?

phranque




msg:4521389
 2:55 am on Nov 22, 2012 (gmt 0)

not likely related to your problem, but you should add some anchors to your patterns.

it would be more efficient if you could make the pattern in the RewriteRule more specific.

celebbiz




msg:4521418
 5:19 am on Nov 22, 2012 (gmt 0)

I am a total newb with .htaccess rules but I know Rewrite is on as there is other simple redirects in there and W3 cache rules, gzip and want not. There is no pattern to the sub.domains as one could be blue.mydomain.com and another one could be tool.mydomain.com etc... Do you guys have an example of what would be best and work for me?

lucy24




msg:4521458
 7:26 am on Nov 22, 2012 (gmt 0)

I know Rewrite is on as there is other simple redirects in there and W3 cache rules, gzip and want not

Do any of them use mod_rewrite? Of course you've got it installed; that's not the worry. But unlike most things in Apache it isn't inherited. You have to turn on the RewriteEngine in each separate htaccess that uses it. Your "simple redirects" may be using mod_alias ("Redirect" or "RedirectMatch" by that name rather than "RewriteRule...").

it would be more efficient if you could make the pattern in the RewriteRule more specific.

I don't think he can. He's redirecting entire subdomains. So unless each one has its own unique set of directory names, you can't do much in the Rule.

Although come to think of it: Do the subdomains contain anything besides pages? Images, stylesheets, scripts and so on? If not, you can constrain all your RewriteRules to requests in / or .html (or whatever your extension is). In fact even if there did use to be other stuff in the subdomains, I can't imagine you'll get a lot of independent requests for them. So you can say something like

RewriteCond %{HTTP_HOST} !^(www\.example\.com}$
RewriteRule \.(css|js|jpe?g|png|gif) - [G]

throwing in all your non-page extensions, and give them a generic Gone.

Still, 100 separate sets of redirects. You are probably better off detouring via a php script which feeds in the information. Then your htaccess will just say something like

RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^\w+\.example\.com
RewriteRule ^(.*)$ /fixup.php?$1 [?]

where [?] means that g1smd or someone like him is going to come along and explain how you rewrite to a different host-- in this case to your primary domain instead of the requested subdomain-- without having it turn into a redirect. You don't want to end up with two redirects if you can help it.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved