Forum Moderators: phranque

Message Too Old, No Replies

Showing path for subfolders under subdomain

browser showing full path for subfolder

         

smiehu

10:51 pm on Dec 1, 2008 (gmt 0)

10+ Year Member



Hi everyone,

I'm trying to redirect to subdomain and subfolders.

www.subdomain.example.com

maindirectory (for example.com)
maindirectory/.htaccess
maindirectory/dir
maindirectory/dir/subdomain
maindirectory/dir/subdomain/index.html
maindirectory/dir/subdomain/folder_1
maindirectory/dir/subdomain/folder_1/index.html

My htaccess file:


Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www.example.com$ [OR]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^dir/subdomain($¦/.*$) http://subdomain.example.com$1 [R=permanent,L]

RewriteCond %{HTTP_HOST} ^www.subdomain.example.com(.*)$ [OR]
RewriteCond %{HTTP_HOST} ^subdomain.example.com(.*)$
RewriteCond %{REQUEST_URI} !^/dir/subdomain
RewriteRule ^(.*)$ dir/subdomain/$1 [L]

There is a folder in subdomain folder. Lets call it 'folder_1'

Everything works fine for all requests from browser:

subdomain.example.com
and
subdomain.example.com/folder_1/

BUT

when I write: subdomain.example.com/folder_1 (without backslash)
browser is showing all path: subdomain.example.com/dir/subdomain/folder_1/

Does anyone know what is causing problem like this? Thank you for your help.

jdMorgan

12:35 am on Dec 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The likely cause of this is the action of mod_dir -- See the Apache mod_dir documentation.

The solution is to fix-up URLs which are missing a trailing slash before mod_dir takes action on them.

You also need to clean up a few other potential issues. I'd suggest:


Options +FollowSymLinks +Indexes
RewriteEngine on
#
# Externally redirect direct client requests for subdomain folders
# in the main domain to the subdomain root
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^dir/subdomain/?(.*) http://subdomain.example.com/$1 [R=301,L]
#
# If requested URL-path in main domain resolves to an existing
# directory but is missing a trailing slash
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteCond %{REQUEST_FILENAME} -d
# Externally redirect to add a trailing slash
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.com/$1/ [R=301,L]
#
# If requested URL-path in subdomain resolves to an existing directory
# but is missing a trailing slash
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com
RewriteCond %{DOCUMENT_ROOT}/dir/subdomain/$1 -d
# Externally redirect to add a trailing slash
RewriteRule ^(([^/]+/)*[^./]+)$ http://subdomain.example.com/$1/ [R=301,L]
#
# Externally redirect requests for non-canonical main domain to canonical main domain
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} !^(www\.)?subdomain\.example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Externally redirect requests for non-canonical subdomain to canonical subdomain
RewriteCond %{HTTP_HOST} !^subdomain\.example\.com$
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule (.*) http://subdomain.example.com/$1 [R=301,L]
#
# Internally rewrite subdomain requests to the subdomain folder
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$
RewriteCond $1 !^dir/subdomain/
RewriteRule (.*) /dir/subdomain/$1 [L]

Note that some of the hostnames in the RewriteCond patterns are end-anchored and some are not. This is intentional, and is critical to the proper functioning of the rules.

I removed the RewriteBase directive, because it's not needed if the RewriteBase is "/", which is the default value. I also combined your Options directives into one line.

There are many other potential canonicalization problems such as direct requests for index.html, spurious query strings appended to URLs, etc. that you might want to consider. However, the rules above will take care of most common domain canonicalization problems such as extra or missing "www" and appended port numbers or FQDN-format hostnames. If your site has a unique (non-shared) IP address, you might also want to add a test for that to the "canonicalize main domain" and "add trailing slash in main domain" rules.

Jim

[edited by: jdMorgan at 12:41 am (utc) on Dec. 2, 2008]

smiehu

12:57 am on Dec 2, 2008 (gmt 0)

10+ Year Member



Wow! Amazing, you have answered my question and gave ready solution!

It's working perfectly.

I should send you flowers, but at the moment I will say thank you guys!