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

Apache Web Server Forum

    
301 Redirect subdomain to folder
Can't seem to get it right
stu2




msg:4158086
 2:26 am on Jun 24, 2010 (gmt 0)

I have two statements in my .htaccess file trying to redirect a subdomain to a folder, but I can't seem to get it right. I'm only using 1 of these at a time. I've put a space after the // so it displays correctly. What's wrong with these?

RewriteEngine On
RewriteCond %{HTTP_HOST} ^forums.mydomain.us$ [OR]
RewriteCond %{HTTP_HOST} ^www.forums.mydomain.us$
RewriteRule ^(.*)$ http:// www.mydomain.us/forum/index.php [L,R=301]

Redirect 301 forums.mydomain.us http:// mydomain.us/forum/index.php

Whats happening is I'm being redirected to another domain I'm hosting on the same host.

If I can't get this right, then if it would produce my Error 404 document instead, it would be good enough.

 

stu2




msg:4158198
 6:54 am on Jun 24, 2010 (gmt 0)

Would it be possible to substitute the wildcard character "*" instead of "forums" in the source, which would all then get redirected to /forum/index.php?

jdMorgan




msg:4158507
 3:58 pm on Jun 24, 2010 (gmt 0)

Totally confused due to too many errors and problems:

> trying to redirect a subdomain to a folder
Why would you want to do that? It loses all advantages of using the subdomain in the first place.

> Whats happening is I'm being redirected to another domain I'm hosting on the same host.
Why? Are the redirect target addresses wrong, or is there some other problem outside of this code?

You're mixing RewriteRules and Redirect 301 directives -- A practice that several contributors here specifically warn against. Further, it seems that you have confused the functions of redirects and rewrites here.

A redirect is a URL-to-URL translation. It says to the client (browser or search engine robot) "The resource you have requested has moved. Please ask for it again at this new URL." This server response terminates the current HTTP request, and the client must begin a new one, using the URL provided by the server in its redirect response.

Further, search engines will take a 301 redirect as a request to discard the original URL and use only the new one for search listings and ranking.

A rewrite is a URL-to-server-filepath translation, and occurs completely 'inside' the server, and in the context of the current (original) HTTP request. All it does is to change the server filepath associated with the requested URL, and its operation is not visible to the client.

So it seems to me that you want an internal rewrite here, and not an external redirect.

Further you should use (link to) and accept only one canonical subdomain for these forum requests. All non-canonical requests should be redirected to the canonical subdomain before the internal rewrite is invoked.

Lastly, there is no good reason to use the "index.php" filename in the forum redirect. In fact, there are several good reasons not to. Map the /forum/ URL-path to /forum/index.php using DirectoryIndex, and then link only to forums.example.com/

I'd suggest something like this:

# Define directory index page filepath
# Optional -- may already be previously-defined by server confiog, and therefore not required
DirectoryIndex index.php
#
# Enable rewriting engine
RewriteEngine on
#
# Externally redirect direct client requests for /forum/index.php to forums.example.com
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /forum/index.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^forum/index\.php$ http://forums.example.com/ [R=301,L]
#
# Externally redirect direct client requests for /index.php in any
# directory to "/" in that same directory (for the forums subdomain)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*/index.php([#?][^\ ]*)?\ HTTP/
RewriteCond %{HTTP_HOST} ^([^.]\.)?forums\.([^.]\.)?mydomain\.us
RewriteRule ^(([^/]+/)*)index\.php$ http://forums.example.com/$1 [R=301,L]
#
# Externally redirect direct client requests for /index.php in any
# directory to "/" in that same directory (for the main domain)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*/index.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect direct client requests for /forum/<anything> to
# forums.example.com/<anything> (applies to all requested domains)
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /forum/[^\ ]*\ HTTP/
RewriteRule ^forum/(.+)$ http://forums.example.com/$1 [R=301,L]
#
# Externally redirect non-canonical forum subdomain requests to canonical forum subdomain
RewriteCond %{HTTP_HOST} !^(forums\.example\.com)?$
RewriteCond %{HTTP_HOST} ^([^.]\.)?forums\.([^.]\.)?mydomain\.us
RewriteRule ^(.*)$ http://forums.example.com/$1 [R=301,L]
#
# Externally redirect other non-canonical domain requests to canonical main domain
RewriteCond %{HTTP_HOST} !^((forums|www)\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Internally rewrite requests for forums subdomain to /forum subdirectory filepath
RewriteCond $1 !^forum/
RewriteCond %{HTTP_HOST} ^forum\.example.com
RewriteRule ^(.*)$ /forum/$1 [L]

Note that the comments are quite specific, and rule order is critical.

The first four rules are "somewhat optional." They can be used to 'correct' search engine listings that are already showing the /forum/ subdirectory or /index.php filepaths as URLs. You can comment them out to test the other three more-important rules.

Hopefully, no typos... :)

Jim

stu2




msg:4158869
 11:18 pm on Jun 24, 2010 (gmt 0)

Further, search engines will take a 301 redirect as a request to discard the original URL and use only the new one for search listings and ranking.


That is exactly what I want to do. The forum doesn't exist on the subdomain any more, it's included in a folder, precisely because I want the domain name to benefit from any SEO which might be derived from it's content, not the separate subdomain.

So it looks like I only need the last one. I'll check it out. Thank you.

stu2




msg:4158875
 11:32 pm on Jun 24, 2010 (gmt 0)

Still every subdomain is being redirected to another domain on my host. Here is my .htaccess file...

<Files .htaccess>
Deny from all
</Files>

ErrorDocument 404 http:// www.mydomain.us/error404.php

RewriteEngine On

# Internally rewrite requests for forums subdomain to /forum subdirectory filepath
RewriteCond $1 !^forums/
RewriteCond %{HTTP_HOST} ^forums\.mydomain.us
RewriteRule ^(.*)$ /forum/$1 [R=301,L]

and here is the .htaccess file from my root directory on the host..

# -FrontPage-

IndexIgnore .htaccess */.?* *~ *# */HEADER* */README* */_vti*

<Limit GET POST>
order deny,allow
deny from all
allow from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>
AuthName myhostname.com
AuthUserFile /home/myhostname/public_html/_vti_pvt/service.pwd
AuthGroupFile /home/myhostname/public_html/_vti_pvt/service.grp

And here is .htaccess from the domain being passed to..

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

jdMorgan




msg:4158932
 2:41 am on Jun 25, 2010 (gmt 0)

The ErrorDocument syntax shown will result in a 302-Found redirect to error404.php, instead of serving the error404.php-generated content along with the desired/required 404-Not Found status. This is a potential "SEO disaster." Remove the protocol and hostname and refer only to a local fielpath to avoid this. See Apache core ErrorDocument documentation.

The redirect to /forum (first rule) does not specify a protocol and hostname, so the default ServerName will be used. If this is the 'other domain' you mention, then you can likely fix the problem by specifying a canonical URL as the redirect target.

In your final rule, consider excluding those filetypes which cannot be generated by the rewrite target scripts. Examples might include image, CSS, and external JS files, document (e.g. .pdf, .xml, and .doc) files, and media files (e.g. .swf, .flv, .avi, etc.). By explicitly matching these filestypes in the first rewritecond and thereby preventing unnecessary 'exists' checks, you may see a dramatic increase in server performance (and possibly disk life as well).

[added] Consider the use of <LimitExcept GET POST> instead of <Limit PUT DELETE>. This closes a hole that leaves several other HTTP methods open without a policy. [/added]

Jim

stu2




msg:4158946
 3:39 am on Jun 25, 2010 (gmt 0)

I'm sorry, but you are talking in language which I'm having a hard time to understand.

As for the error 404. So I should rewrite this as

ErrorDocument 404 /error404.php
or this
ErrorDocument 404 error404.php

So what should this redirect to /forum (first rule) be?

And what should the final rule look like?

And I should delete the <Limit PUT DELETE> command?

jdMorgan




msg:4159006
 7:22 am on Jun 25, 2010 (gmt 0)

> should rewrite this as
ErrorDocument 404 /error404.php

> So what should this redirect to /forum (first rule) be?
# Externally redirect requests for forums subdomain to /forum subdirectory
RewriteCond %{HTTP_HOST} ^forums\.example\.us
RewriteRule ^(forum/)?(.*)$ http://www.example.com/forum/$1 [R=301,L]


> And what should the final rule look like?
This varies based on what types of resources can be generated by your scripts. I don't know about your scripts, but a typical site might use:

# Skip the following rules if any of these filetypes are requested or if
# the requested URL resolve to a physically-existing file or directory
RewriteCond %{REQUEST_URI} \.(gif|jpe?g|png|ico|css|js|pdf|swf|flv|avi|wmv)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

It is reasonably certain that your scripts do not generate image files -- They may generate HTML pages which 'include' images, but likely do not generate the image files themselves. Therefore, skipping the file- and directory- exists checks along with the final three rules is generally safe. The other filetypes are also not likely to be generated by typical PHP scripts.

The idea here is to avoid doing the file- and directory-exists checking when it is not necessary; If the current state of the filesystem is not cached in memory, or if that cache is marked as 'dirty,' the those two -f and -d RewriteConds represent physical disk reads. Now humans consider those to be fast, but compared to HTTP request transmission and normal server response handling, disk reads are fairly slow. Plus, every unnecessary disk access you allow is one less necessary one you'll be able to do before that hard drive (eventually) fails...

The bottom line is that by excluding the most-frequently-requested filetypes which cannot be generated by the scripts, you avoid beating your hardware to death and also speed up your site. On many sites, this one small change makes a visible improvement in site responsiveness under load. On lightly-loaded sites, you may see no change except that the disk activity light seems slightly 'dimmer' because there are fewer disk accesses occurring.

> And I should delete the <Limit PUT DELETE> command?
No. Replace the <Limit PUT DELETE> and its closing </Limit> with
<LimitExcept GET POST> and a closing </LimiteExcept>,
leaving the enclosed content of the original <Limit PUT DELETE> container as it was.

This takes care of *all* other HTTP methods *except* for GET, HEAD, and POST (note that HEAD is considered as a variant of GET, and is therefore 'automatically included' with GET). For example, your original code had nothing to say about CONNECT, PROPFIND, and a bunch of other HTTP methods. The suggested approach says, "Do this (allow them) with GET, HEAD or POST and if it isn't one of those, then do this other thing (deny them)."

Jim

stu2




msg:4159553
 10:01 pm on Jun 25, 2010 (gmt 0)

1,2, and 4 - check.

3. This may be simple to you, but I'm having a hard time getting my head around it.

First: Is the "?" in "jpe?g" a typo?
Second: What is RewriteRule ^ - [L] saying? Do nothing?
Third: The last 3 lines. I should replace these directories with whatever directories I have on my website?

PS: I have a lightly loaded website.

stu2




msg:4159609
 12:33 am on Jun 26, 2010 (gmt 0)

Putting 4. into my .htaccess file gave me a Error 500 on my entire website.

All subdomains are still redirecting to another domain on my hosting. I thing it's time to ask my Hosting.

jdMorgan




msg:4159680
 3:59 am on Jun 26, 2010 (gmt 0)

First: Is the "?" in "jpe?g" a typo?

Basic regular expressions. "?" is a quantifier that says "Match zero or one of the preceding character, bracketed character group, or parenthesized sub-expression." It therefore makes the "e" optional, so that subpattern matches either "jpeg" or "jpg."

Second: What is RewriteRule ^ - [L] saying? Do nothing?

"Match any requested URL-path and if the preceding RewriteConds are true, leave the URL-path unchanged and stop mod_rewrite processing here."

Third: The last 3 lines. I should replace these directories with whatever directories I have on my website?

I have no idea. Those directories first appear in code you posted, and I presume they are "common" subdirectories associated with WordPress.

I don't know what you mean by "4" above. If you are saying that changing the <Limit PUT DELETE> container to a <LimitExcept GET POST> container causes 500-Server Errors, then there's either a typo or your server is toast... The former is much more likely, especially since it looks like I double-typed the "e" in the closing "</LimitExcept>" directive above.

Jim

stu2




msg:4160125
 11:16 am on Jun 27, 2010 (gmt 0)

OK. I fixed the forwarding. What my hosting said was I had to firstly change to their servers (after another help tech told me to set an A Address Record to my own hosting IP Address at my registrar), and then had to re-create the forum sub-domain, then had to use their cPanel to redirect the forum to the new place. No .htaccess redirect appeared to work. It also means I can't do a blanket sub-domain redirect to my /forum/, which would have been kinda nice catch-all, since I'm not sure what other sub-domains the previous owner had.

Those folders were in the .htaccess file of the domain which was being redirected to. not in the .htacess file of the domain under discussion. which was why I asked if I had to change the names of the folders to those of the problem domain. Did that make sense?

jdMorgan




msg:4160247
 4:59 pm on Jun 27, 2010 (gmt 0)

If you use cPanel to "re-create the forum subdomain" as a 'add-on domain,' then it will exist sort of 'on its own' -- The top-level .htaccess file in your 'main domain' won't be executed when requests are made to the forum subdomain.

As a result, you may have to split up and/or duplicate some of the rules based on which domain/subdomain they are to be applied to.

Also, scripts and files in the add-on domain's filespace won't have access to scripts and files in the main domain's filespace, as it is generally not allowed to 'go above documentroot' in these set-ups.

Jim

stu2




msg:4162419
 10:55 pm on Jun 30, 2010 (gmt 0)

I would just like to say thank you for all the help you have provided. Thank you.

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