Correct way of running site from sub-directory?

Discuss practical ways rearrange URLs using mod_rewrite.

Correct way of running site from sub-directory?

Postby rover » Sat Oct 04, 2008 3:42 am

For example we want anyone going to

www.ourdomain.com

to automatically be taken to

www.ourdomain.com/forum

I know we could host the forum from the root and not use a sub-directory, but we actually want to have the sub directory as the root of our site.

At the moment our rewrite stuff (contained in an .htaccess in our root) reads:

Code: Select all
Options +FollowSymLinks

# Turn on rewrites.
RewriteEngine on

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

# Only apply to URLs on this domain
RewriteCond %{HTTP_HOST} ^(www.)?ourdomain.com$

# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI} !^/forum/

# excluding requests for existing files
RewriteCond %{SCRIPT_FILENAME} !-f
# excluding requests for existing directories
RewriteCond %{SCRIPT_FILENAME} !-d
# redirect to the root of your website.
RewriteRule . /forum/? [R,L]

# Also redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?ourdomain.com$
RewriteRule ^(/)?$ /forum/index.php [R=301]


This seems to work pretty well, but the problem is that Google (and other SEs) are looking for our robots.txt file in the root and because of the rewrite it throws up an error.

I was wondering what we should do? Is this the best way of hosting a site from a sub-directory? Can the robots.txt issue be fixed?

Thanks in advance.
rover
 
Posts: 16
Joined: Mon Dec 18, 2006 7:24 pm

Postby richardk » Sat Oct 04, 2008 7:10 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(robots\.txt)$ /forum/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^forum(/.*)?$ http://www.example.com/forum/? [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://www.example.com/forum/? [R=301]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.ourdomain.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rover » Sat Oct 04, 2008 8:05 am

Hi Richard, great to 'meet' you again - I remember you helped me a while ago... you really are a good person to spend all this time helping people for free :)

What is your opinion on this issue of running a site from a sub-directory? Is it going to mean I do poorly in search engines for example? I just want to ensure that I have the word 'forums' in my URL as it's not in my site domain.

With the code you supplied, what are the main changes you made? I'm just curious and keen to learn.

Thanks
rover
 
Posts: 16
Joined: Mon Dec 18, 2006 7:24 pm

Postby richardk » Sun Oct 05, 2008 11:02 am

What is your opinion on this issue of running a site from a sub-directory? Is it going to mean I do poorly in search engines for example? I just want to ensure that I have the word 'forums' in my URL as it's not in my site domain.

You'd have top ask a SEO person.

With the code you supplied, what are the main changes you made? I'm just curious and keen to learn.

I added
Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(robots\.txt)$ /forum/$1 [L]

so requests to /robots.txt go silently to /forum/robots.txt.

I replaced
Code: Select all
RewriteCond %{REQUEST_URI} !^/forum/

with
Code: Select all
!^forum(/.*)?$

It's the same (as long as the .htaccess file is in your document root) but doesn't use a RewriteCond.

I moved
Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.ourdomain.com/$1 [R=301,L]

to the end because the other redirects add www.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rover » Mon Oct 06, 2008 4:40 am

Hi Richard,

I tried your solution above and IE reports this error:
Redirect Loop

Redirection limit for this URL exceeded. Unable to load the requested page. This may be caused by cookies that are blocked.

This is when I entered www.ourdomain.com and in theory it should have taken me to www.ourdomain.com/forum

Any thoughts on what's causing this?
rover
 
Posts: 16
Joined: Mon Dec 18, 2006 7:24 pm

Postby richardk » Mon Oct 06, 2008 11:20 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(robots\.txt)$ /forum/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule !^forum(/.*)?$ http://www.example.com/forum/? [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://www.example.com/forum/? [R=301,L]

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


Or
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(robots\.txt)$ /forum/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/forum(/.*)?$ [NC]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule .* http://www.example.com/forum/? [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://www.example.com/forum/? [R=301,L]

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.ourdomain.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rover » Tue Oct 07, 2008 2:46 am

Sorry to be a pain in the neck Richard, but both of these lead to redirect loop errors.

Just to confirm what I'm after:

All requests for www.ourdomain.com automatically get forwarded to www.ourdomain.com/forum - effectively making /forum the root of the entire web site, including robots.txt

If you have any more time to donate it'd be greatly appreciated :)
rover
 
Posts: 16
Joined: Mon Dec 18, 2006 7:24 pm

Postby richardk » Tue Oct 07, 2008 11:22 am

Damn copy and paste
Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

should be
Code: Select all
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby rover » Tue Oct 07, 2008 2:37 pm

Hi Richard, I went for your first suggestion above (with the later correction) and it seems to be working fine, thanks. Is there any major reason to pick either one of your suggestions over the other?

Also, I have tried to revalidate my robots.txt file, but Google are saying it has to be in the root directory, so I guess, after all this, that I actually need a rule that will enable me to keep the robots.txt file in the root. Is there a way of doing that while redirecting all other requests to /forum?
rover
 
Posts: 16
Joined: Mon Dec 18, 2006 7:24 pm

Postby richardk » Wed Oct 08, 2008 8:51 am

I went for your first suggestion above (with the later correction) and it seems to be working fine, thanks. Is there any major reason to pick either one of your suggestions over the other?

The second one (with the modification to stop it looping) is the best. The first one is missing an L flag on the third rule. The third one has RewriteConds that are not needed.

Also, I have tried to revalidate my robots.txt file, but Google are saying it has to be in the root directory

With my mod_rewrite Google should think the robots.txt file is in your document root. Do you get redirected if you visit /robots.txt? You may need to replace
Code: Select all
!^forum(/.*)?$

with
Code: Select all
!^(forum(/.*)?|robots\.txt)$


after all this, that I actually need a rule that will enable me to keep the robots.txt file in the root. Is there a way of doing that while redirecting all other requests to /forum?

You should be able to do that by just removing
Code: Select all
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(robots\.txt)$ /forum/$1 [L]

Requests to existing files are not redirected (because of the !-f RewriteCond).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 24 guests

cron