Forum Moderators: phranque

Message Too Old, No Replies

Redirecting main URL to a sub-folder

         

aeam

2:44 am on Jul 4, 2009 (gmt 0)

10+ Year Member



Please bear my ignorance here. I'm pretty lost here though it is probably a very simple thing. At this point I'm really curious to know how exactly it is done (i've been trying with webmin with no luck and tried a rewrite rule .. to no effect)

problem:

I want to point my main url http://example.com/ to the location where my forum is installed: http://example.com/smf

Webmin:

I tried to use the "Temporary URL redirect" option provided in Webmin under my default virtual server but it doesn't seem to have any effect. this is what I set it to:

from: http://example.com/
to: http://example.com/smf

httpd.conf:

So I tried this in httpd.conf :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ smf
</IfModule>

I didn't put the / in the rewrite rule because from what I gathered the 'pattern' is whatever comes after the / and in my case it is nothing. and that is supposed to match to smf/ ..

so basically I don't know what the heck I'm doing because this is probably the simplest thing that people do on a daily basis.

Please excuse my ignorance. It is probably not wise to redirect your main site to a subfolder like I'm doing, and when I do get a forum running (simple machines) I will put it in the main directory. But right now, I installed in the subdir by a fluke thinking I could pretty easily redirect the links to main site down one folder but haven't been able to figure out.

What is the proper way of doing this?

jdMorgan

3:29 am on Jul 4, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The proper way of doing it is to install the forum at the directory level where you want it to reside, rather than applying "band-aids" after the fact. Using redirects (especially 302-Temporary redirects) has side-effects because of the treatment of redirects by search engines, and you probably will not like those side-effects.

If you simply want to partition off your "file storage space" to keep things neat inside the server while not 'showing' this partitioning in the URL, then that calls for an internal URL-to-filepath rewrite, not an external URL-to-URL redirect.

Internal rewrites do not inform the client (e.g. browser or search engine robot) that anything has changed, they simply 'map' URLs into the filesystem in a non-default way, saying in essence, "If the URL says to get the content from here in the filesystem, then get it from there in the filesystem instead."

On the other hand, an external redirect causes the server to respond to the initial HTTP request with a message that effectively says, "The resource you have requested has moved. Please ask for it again at this new URL."

Therefore, every HTTP request for a redirected URL results in that request being terminated with a 30x server status response, and the client (browser or robot) must issue a second HTTP request using the new URL to get what it wanted in the first place. So, your site acts slow, and your log and statistics files are 'polluted' with two HTTP requests logged for every page, image, external CSS and JS script file, etc. requested from your server.

This is not a good way to start off with a new forum or site...

If you are just getting into this, I'll offer the opinion that your server is your site's "house." If you build that house on sand, it will sink. Take the time to seek authoritative documentation, and study it. No, it's not easy, but it will save you from many expensive mistakes -- some of which can easily put you out of business...

That is not to say you cannot "do it yourself." You can, but it takes considerable research. Links to the Apache mod_rewrite documentation and a Regular-Expressions tutorial are available in our Apache Forum Charter [webmasterworld.com]. Cautionary tales and horror stories about common errors abound in our "Google News" and other Search-Engine-specific forums, and these are well worth reading as well. My favorite thread title to date is "Duplicate Content -- Get it Right or Perish."

---

In httpd.conf, but not inside any <Directory> container, the URL-path seen by RewriteRule *will* start with a slash, and you should always add an [L] flag to every rule, unless you have a very specific reason not to do so.

If you fully-anchor your pattern (as you did) and create no back-references, then only requests for the (single) directory-index URL will be re-written to your subdirectory; No other URL-path requests will be affected. It is likely that you want to rewrite *all* URL-paths meeting some criteria to your subdirectory. If so, then those criteria must be specified in the RewriteRule and/or RewriteConds, and you will likely want to back-reference any additional URL-path information to "copy" it into the modified subdrectory path.

For example, rewriting "/" to "/subdir/" and rewriting "/foo" to "/subdir/foo" is a common apporach. This is exemplified by code like

 RewriteRule ^post/(.*)$ /forum/post/$1 [L] 

where the "post" string in the requested URL-path indicates a URL-class that should be rewritten to the /forum subdirectory, and the path info following "post/" is copied from the first parenthesized sub-pattern into the new path using the first local variable $1.

Also, be aware that as documented, you must enable either the FollowSymLinks or SymLinksIfOwnerMatch Option in order to use mod_rewrite. If this is not done, you will get a 500-Server Error, and your server error log will contain a message specifying that one of these options must be enabled to use mod_rewrite.

Have fun, but study and go slow... Know what you want to do, why you want to do it, and what the side-effects of doing it might be before rushing directly to coding and implementation.

Jim

aeam

1:28 pm on Jul 4, 2009 (gmt 0)

10+ Year Member



Jim, Thank you so much for such a detailed and informative answer. I am indeed rather new at this url-rewriting thing and the best thing that came out of my mistake and ensuing google searches is that I discovered this site which seems to me a treasure trove of information.

Eventually I want to have a main url and then /forum /blogs /classifieds etc as needed and I figured while I was only doing the forums first, I could just redirect the main site's traffic to the forums ... but I see now that that's not such a good idea (at least via 30x etc.)

What I realized after reading your post is that I'm not thinking ahead from a google and SEO perspective (another area I need to read up on quickly.)

I think I want to do the "internal rewrite" as opposed to the double-whammy redirect business which seems like the incorrect thing to do in this particular situation.

Thanks also for suggesting the resources, I am in the process of reading various "tutorials" and whatnot and playing around with it on a temporary ec2 server.

At this point it is academic for me to figure out how I would do it *if I needed to do it* (though it is not the right thing to do). BTW, I did something like this at godaddy through their panel and I think all it did was do a 301 redirect.

I have a lot of learning to do, this much is clear. :)