homepage Welcome to WebmasterWorld Guest from 54.227.215.139
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 59 message thread spans 2 pages: 59 ( [1] 2 > >     
301 using .htaccess including subdomains
Seawitch



 
Msg#: 4609782 posted 4:32 pm on Sep 14, 2013 (gmt 0)

Hi all,

I'm puzzled, as the techs at the hosting company made the changes, but they're not working. Between them and me, we've all been beating our heads in to a wall. LOL.

In any case, what I'm looking to do is to redirect 301 from the old domain to the new, INCLUDING the subdomains (the structure is identical, domain to domain), while having both hosted on the same dedicated server.

Here is what I have:

Options +FollowSymlinks

DirectoryIndex index.html index.htm index.php

RewriteEngine on
RewriteBase /
#RewriteRule !redirect.html /redirect.html
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_HOST} ^(.*)example\.net [NC]
RewriteRule (.*) http://%1example.org%{REQUEST_URI} [R=301,L]


I'm sure there's an error somewhere, but where?

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4609782 posted 5:01 pm on Sep 14, 2013 (gmt 0)

The code must be placed where every sub-domain will resolve to it.

Is it in .htaccess somewhere or in the main config file?

Seawitch



 
Msg#: 4609782 posted 5:19 pm on Sep 14, 2013 (gmt 0)

In .htaccess, at the root of the old domain.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 5:28 pm on Sep 14, 2013 (gmt 0)

The first condition says if the referrer is not empty -- That means any type-in, bookmark click or search engine will not be redirected. It could be the issue if you're not clicking a link to check the redirect and even if it's not the issue I'd definitely remove it.

Also, make sure you empty your browser cache between every test.

There are a couple of efficiency changes I made:

([^.]+\.) = Is not a . (dot) 1 or more times, followed by a . (dot)
([^.]+\.)? = The ? makes the entire pattern optional.

(.*) = match and store the entire request, but we don't use the entire request anywhere, so we don't need to match and store the whole thing like they were doing in the original rule -- The only thing we need to do is:

.? = 0 or 1 character

[NC] in the HTTP_HOST is unnecessary -- Browsers automatically correct it to lower case prior to sending the request even if someone types in all caps.

RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.net$
RewriteRule .? http://%1example.org%{REQUEST_URI} [R=301,L]

BTW: Welcome to WebmasterWorld!

Seawitch



 
Msg#: 4609782 posted 5:49 pm on Sep 14, 2013 (gmt 0)

Options +FollowSymlinks

DirectoryIndex index.html index.htm index.php

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.net$
RewriteRule .? http://%1example.org%{REQUEST_URI} [R=301,L]

^
Like that?

Glad I found you guys!

[edited by: phranque at 10:23 pm (utc) on Sep 14, 2013]
[edit reason] unlinked url [/edit]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4609782 posted 6:01 pm on Sep 14, 2013 (gmt 0)

I suspect that the requests for subdomains do not even resolve to this file for processing.

Test it out by adding an image in the same folder and then try to access that image using various domain and sub-domain requests.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4609782 posted 6:06 pm on Sep 14, 2013 (gmt 0)

<aside>
[NC] in the HTTP_HOST is unnecessary -- Browsers automatically correct it to lower case prior to sending the request even if someone types in all caps.

... and it's unnecessary in referers (for example hotlink protection) for a different reason: If the casing is wrong you know it's forged.
</aside>

The first condition says if the referrer is not empty -- That means any type-in, bookmark clicks or search engine will not be redirected.

Add that to the list of "D'oh!" explanations that someone, somewhere will eventually forget :)

If you're excluding blank referers you might as well also constrain the rule to pages, for example
(^|\.html|/)$
Any request for a non-page file reaching your old site can only be a robot, most likely a search engine, since everyone else will already have been redirected.

Do requests for the new domain pass through the same htaccess as requests for the old one? The Referer line about HTTP_HOST implies that they do-- unless you've simply got other, unrelated domains that you need to exclude. Incidentally, the referer is most efficiently expressed as
^(\w+\.)?example\.com
or
^([^.]+\.)? et cetera

If none of your subdomain names happen to start in "e" --where "e" represents the actual first letter of your actual domain name-- you can even say
^([^e][^.]*\.)? et cetera

Seawitch



 
Msg#: 4609782 posted 6:19 pm on Sep 14, 2013 (gmt 0)

I'm not sure I'm getting all this.

The domains both live on the SAME server, but in different directories ON the server, which is a dedicated server. The file structure, domain to domain, subdomain to subdomain, is identical.

The redirect works at the root, but not for the subdomains.

Would it work better if I did an .htaccess file in each subdomain, at the subdomain root for those following outdated links? If so, how would I structure that, as it would be going to the new domain? (IF I can get it to work, LOL)

Seawitch



 
Msg#: 4609782 posted 7:08 pm on Sep 14, 2013 (gmt 0)

RewriteCond %{HTTP_REFERER} ^(\w+\.)?example\.com is what I should use then?

The other questions I had was this. The redirect goes from the root of the old, to the root of the new, BUT, if I input an old page address, it stays at the old domain, even though all the pages are also named identically.

Seawitch



 
Msg#: 4609782 posted 7:51 pm on Sep 14, 2013 (gmt 0)

Oh yes, forgot to mention that there are 23 subdomains, which are listed in the NEW domain .htaccess, but not in the old one at present.

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4609782 posted 8:29 pm on Sep 14, 2013 (gmt 0)

Do all subdomains work as intended if you request them directly? There doesn't have to be any content yet. What matters is that your browser's address bar says "subdomain.example.com", even if all you see is the 404 page. (A "can't find the server" error message is bad.)

For testing purposes you might change your first condition to the pair

RewriteCond %{HTTP_REFERER} !^-?$ [OR]
RewriteCond %{REMOTE_HOST} ^123\.45\.67\.89$

using your own IP address in the second option. The added -? is just for insurance; it may not technically be necessary. The main objective is to poke a hole for yourself. Caution! The combination of a negative ! condition with the [OR] flag can only be used if you're looking at different properties. Or, OK, a single property that can have multiple pieces, such as a long query string.

Would it work better if I did an .htaccess file in each subdomain

No, definitely not. Do everything possible to avoid having more than one mod_rewrite section on the same path. Do as I say, not as I do.

The only question is whether you have a separate htaccess for each domain, or a single config file for the whole server. My generic advice when you're making major changes is: Temporarily allow overrides in the appropriate <Directory> section(s) of the config file. Now make all your changes in htaccess. This will let you tweak things without having to restart the server each time. Once you're sure everything is working as intended, give things a few days to settle in and then turn off overrides and shift the htaccess parts to the config file, making any necessary translations. If you're a small site it will not make a huge difference. But in general, if you have access to the config file, use it.

The change from .* to (\w+\.)? will not affect rule execution at all. It just creates a slightly more efficient Regular Expression.

there are 23 subdomains, which are listed in the NEW domain .htaccess

Listed in what way? There has to be subdomain-to-directory mapping on both domains, since servers don't operate in four dimensions ;) But that won't happen in htaccess (uh... can it even be done in htaccess?); it's in the config file.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 8:46 pm on Sep 14, 2013 (gmt 0)

Would it work better if I did an .htaccess file in each subdomain...

You actually need to if they have different host directory than the root domain, because in that case the subdomains won't "see" or be "influenced by" the root domain's .htaccess.

If the preceding is the case, just copy/paste the root domain's .htaccess that's working [edited slightly below] into the subdomain's host directory and you should be good.

Note: The RewriteBase is unnecessary since we're simply redirecting, not rewriting anything

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.net$
RewriteRule .? http://%1example.org%{REQUEST_URI} [R=301,L]

[edited by: JD_Toims at 8:49 pm (utc) on Sep 14, 2013]

Seawitch



 
Msg#: 4609782 posted 8:48 pm on Sep 14, 2013 (gmt 0)

Okay, this is what I started with

RewriteEngine on
#RewriteRule !redirect.html /redirect.html
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_HOST} ^(.*\.)?old\.net [NC]
RewriteRule ^(.*)$ http://%1new/$1 [R=301,L]
RewriteCond %{HTTP_REFERER} !^http://www2.old.net/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www2.old.net$ [NC]


There are many more lines for each subdomain aside from the last two in the above.

This is what I've changed it to (so far)

Options +FollowSymlinks

DirectoryIndex index.html index.htm index.php

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^([^.]+\.)?old\.net
RewriteRule .? http://%1new.org%{REQUEST_URI} [R=301,L]


Now, the redirect works, but only to a point. It redirects from the old root to the new root, and one location only. What I WANT it to is to redirect where if a person types in one path (i.e. old.net/xyz.php) it goes to the new domain instead, to new.org/xyz.php

I also want it to redirect per subdomain from one old link to where the new one is. It's the same database, identical file structure, identical folder structure for subdomains; in other words, it's cloned exactly.

So say someone clicks an old bookmark, like http://sub.old.net/anyfile.php?=1234

I need the redirect to work where the link will actually go to http://sub.new.org/anyfile.php?=1234

So, what I need it to do is wildcard the files beneath the directory. If that makes any sense at all.

Remember too, that it's all on the same server. Different domain names, as the old one is no longer in my control, but the FILES, both the original domain files and the new domain files are on the same server. I'm thinking that may be a part of my issue.

[edited by: phranque at 10:24 pm (utc) on Sep 14, 2013]
[edit reason] unlinked urls [/edit]

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 8:53 pm on Sep 14, 2013 (gmt 0)

Different domain names, as the old one is no longer in my control...

Uh, does that mean the old domain [the one you want to redirect] is no longer hosted on your server? -- If that's the case there's no way you can redirect anything by yourself. The new owner of the domain would have to do that for you.

What I WANT it to is to redirect where if a person types in one path (i.e. old.net/xyz.php) it goes to the new domain instead, to new.org/xyz.php

RewriteRule .? http://%1example.org%{REQUEST_URI} [R=301,L]

Is essentially the same as:

RewriteRule (.*) http://%1example.org/$1 [R=301,L]

The difference is rather than storing and back-referencing the location requested [by using (.*)] we're only checking for a character or no characters [by using .?] and then moving on the to condition, because we can use the pre-stored server variable %{REQUEST_URI} rather than double storing the info with (.*).

In-other-words: What I posted should do exactly what you want -- If it's not, make sure you empty your cache, because there's no reason I can see it should not redirect the way you want it to.

Seawitch



 
Msg#: 4609782 posted 9:06 pm on Sep 14, 2013 (gmt 0)

The other person controls the domain name registration only. I have control of all the site files, site hosting, new domain, and etc. That's the whole reason for getting a new domain name, so that when it expires again next year, my users and I are not left hanging.

Seawitch



 
Msg#: 4609782 posted 10:43 pm on Sep 14, 2013 (gmt 0)

UPDATE -

The subdomains now redirect as well, with NO problems. Next step, how do I make old links to dynamic pages redirect to new?

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4609782 posted 10:49 pm on Sep 14, 2013 (gmt 0)

welcome to WebmasterWorld, Seawitch!


please provide exemplified samples of the "old links to dynamic pages" and the equivalent new urls.

Seawitch



 
Msg#: 4609782 posted 11:01 pm on Sep 14, 2013 (gmt 0)

such as
sub.old.net/story.php?=1234567

to

sub.new.org/story.php?=1234567

The subdomains and the domain are all redirecting, that stuff isn't

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 11:06 pm on Sep 14, 2013 (gmt 0)

The subdomains and the domain are all redirecting, that stuff isn't

That still sounds like a caching issue to me -- What browser are you using?

If you can maybe try a "clean" one, especially if you've been using IE.

Seawitch



 
Msg#: 4609782 posted 11:13 pm on Sep 14, 2013 (gmt 0)

I've cleared cache on FF, IE, Chrome, Maxthon, and Safari. I'll try again, restart the machine again, and see what happens.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 11:19 pm on Sep 14, 2013 (gmt 0)

I've cleared cache on FF, IE, Chrome, Maxthon, and Safari. I'll try again, restart the machine again, and see what happens.

WOW, crazy -- If that doesn't work are there other RewriteRules in your .htaccess files or are we looking at the whole thing below?

RewriteEngine on

RewriteCond %{HTTP_HOST} ^([^.]+\.)?example\.net$
RewriteRule .? http://%1example.org%{REQUEST_URI} [R=301,L]

Seawitch



 
Msg#: 4609782 posted 11:33 pm on Sep 14, 2013 (gmt 0)

That's all that's there. I killed the rest of it.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 11:43 pm on Sep 14, 2013 (gmt 0)

Okay, go here: Check HTTP Response Headers [freetools.webmasterworld.com] and enter one of your pages on a subdomain [one of the ones that should be redirected] EG subdomain.example.net/story.php?var=value and please copy/paste your results for us to look at. [Examplifying any "site-identifiable" information of course.].

That should give us not only the status code returned, but also all the additional headers sent by your server. Maybe we'll find some clue as to what's going on in there.

Seawitch



 
Msg#: 4609782 posted 11:54 pm on Sep 14, 2013 (gmt 0)

HTTP/1.1 200 OK
Date: Sat, 14 Sep 2013 23:54:09 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=ilo6apm2knhbfov21m0kq1n6q4; path=/
Vary: Host,Accept-Encoding,User-Agent
Connection: close
Content-Type: text/html

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 12:06 am on Sep 15, 2013 (gmt 0)

Interesting -- Is anyone else seeing anything I'm missing in the ruleset that would cause there to *not* be a redirect for a URL like /story.php?var=val ?

The only things I can think of are caching [which shouldn't be an issue with the Expires, Cache-Control and Pragma returned by the server] and a rule conflict somewhere, which doesn't seem likely either if the page being requested is in the root directory -- I'm a bit stumped here.

Seawitch



 
Msg#: 4609782 posted 12:07 am on Sep 15, 2013 (gmt 0)

the page is in the root of one of the subdomains
that's where all that type of links are.

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 12:25 am on Sep 15, 2013 (gmt 0)

Let's try it with PHP on one page and see what happens.

On a page like http://subdomain.old-domain.net/story.php put this on the lines after <?php:

So, it would look like the following on the page.

###

<?php
error_reporting(0);
header('Location: http://subdomain.new-domain.org'.$_SERVER['REQUEST_URI'].'?'.$_SERVER['QUERY_STRING'],TRUE,301);
exit;

// Everything else below here.

###

Then request http://subdomain.old-domain.net/story.php?var=val again.

Seawitch



 
Msg#: 4609782 posted 12:39 am on Sep 15, 2013 (gmt 0)

HTTP/1.1 200 OK
Date: Sun, 15 Sep 2013 00:38:53 GMT
Server: Apache
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Set-Cookie: PHPSESSID=ntd4aochntoga2o6ajeik0no31; path=/
Vary: Host,Accept-Encoding,User-Agent
Connection: close
Content-Type: text/html

JD_Toims

WebmasterWorld Senior Member Top Contributors Of The Month



 
Msg#: 4609782 posted 12:41 am on Sep 15, 2013 (gmt 0)

Unreal -- That should redirect absolutely everything.

It almost has to be some type of caching somewhere, because the PHP I gave is not dependent on anything like a host header being sent and matching a pattern. It should redirect every request for the page it's on -- Most interesting to me is the Header Checker here doesn't keep a cache, so it should definitely receive a 301, but it's getting a 200 OK instead -- Huh WTF?

Seawitch



 
Msg#: 4609782 posted 12:55 am on Sep 15, 2013 (gmt 0)

Caching in the file setup or my browsers?

This 59 message thread spans 2 pages: 59 ( [1] 2 > >
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