Forum Moderators: phranque

Message Too Old, No Replies

.htaccess -- inability to fix missing "/" at end

         

JohnRuskin

1:53 am on Jan 5, 2011 (gmt 0)

10+ Year Member



The .htaccess code, below quoted from the subdirectory .htaccess, correctly ext. redirects the following browser entry:

www.mydomain.com/subdirectory/


to:
subdomain.mydomain.com/index.html

It does this using the code in the below .htaccess

However a browser entry of the following [note the missing ending slash]:
www.mydomain.com/subdirectory

instead, generates the following odd redirect:
subdomain.mydomain.com//var/.../.../subdirectory

This bit following the "//" is the server's directory

Below the problem rule are the fixes I have attempted, which were variations inserted above the 1st Rule that I quote. The purpose is to catch the missing slash, before it gets to that rule -- these rule attempts were placed into the subdirectory .htaccess. Following those, below, is a rule placed into the ROOT .htaccess, which also does not get executed.

I have deleted the parent/root directory .htaccess, to prove to myself that the rule creating havoc is within the subdirectory's .htaccess. And I determine that the rule in the ROOT, below, is not executed.

I have added odd "#*$!x" to the redirects, to prove which ruleSet is causing havoc.

The server, and the .htaccess in the subdirectory, are behaving as if, on one hand, it knows that the requested file is in the subdirectory, yet gets confused with the missing trailing slash.

I have no access to the server.

Any suggestions?


=====================================
#.htaccess code on the subdirectory:

ErrorDocument 403 missing.html

AddHandler server-parsed .html .htm .shtml .shtm
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine On

## [1st entry] Move references [subfolder on main domain] to subdomain
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^(subdirectory/)?(.+)$ http://subdomain.mydomain.com/$2 [R=301,L]


=====
ATTEMPTED CURES, by adding ONE of the following above the ruleSet, quoted above. [None of these tries worked...] I am attempting to catch the problem URL, discussed above
========


## [Curiosity Try... DOES NOT get executed...to see what is trapped for the "$1"]
###If no trailing slash, and it is THIS subdirectory...add the "/index.html
RewriteCond %{HTTP_HOST} ^www\.complianceofficer\.com$
RewriteRule ^(.?)$ http://education.complianceofficer.com/$1junk [R=301,L]

#2nd curiosity try, which is also not executed with that problem URL
RewriteCond %{HTTP_HOST} ^www\.complianceofficer\.com$
RewriteRule ^(.+)$ http://education.complianceofficer.com/$1 [R=301,L]

## [ 1st try ] If no trailing slash, and it is THIS subdirectory...add the "/index.html -- [to see what the RuleSet finds...]
#RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
#RewriteRule ^(.+)$ http://subdomain.mydomain.com/$1 [R=301,L]

## [ 2nd try ]If no trailing slash, and it is THIS subdirectory...add the "/index.html/
#RewriteCond %{HTTP_HOST} ^www\.complianceofficer\.com$
#RewriteRule ^(subdirectory)$ http://education.complianceofficer.com/$1 [R=301,L]

## [ 3rd try ]If no trailing slash, and it is THIS subdirectory...add the "/index.html/
#RewriteCond %{HTTP_HOST} ^www\.complianceofficer\.com$
#RewriteRule ^(subdirectory)$ http://education.complianceofficer.com/index.html [R=301,L]

## other variants:
#using !^(.+)$ or !(.+) in the RewriteRule...[not any character...]


==============
ATTEMPTED CURE by the following entry in the ROOT .htaccess does not cure
=============
## If NOT a trailing slash and it IS a valid subdirectory....add the slash
RewriteCond $1 !(/$)
# and if URL with appended slash DOES exist as a directory
RewriteCond %{DOCUMENT_ROOT}/$1/ -d
# THEN externally redirect to subdir/index.html
RewriteRule ^(.+)$ http://www.complianceofficer.com/$1/ [R=301,L]

jdMorgan

8:25 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do not allow multiple rules (regardless of which .htaccess file they may be in) to execute for the same request; doing so will often result in your internal filepaths being revealed as URLs to the client. You want to put one rule --in the highest-level .htaccess file required for it to work-- that will handle all 'problems' with the URL all in one go. This eliminates the need to have one rewrite's output invoke another rewriterule.

In addition to the filepath-exposure problem, having more than one rule modify a request also can trigger a known mod_rewrite-in-.htaccess bug which was reported years ago but remains un-corrected to this day.

There is a very-ugly work-around that can be applied if it is really ever necessary to apply more than one rule to a single request, but it's so ugly and complicated that I don't feel like discussing it here unless there is truly no other solution -- And that is highly unlikely.

It may be as simple as this one rule:

## Externally redirect requests for main-domain/subfolder/anything to subdomain/anything
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^subdirectory(/(.*))?$ http://subdomain.example.com/$2 [R=301,L]

Here, requests for "/subdirectory", "/subdirectory/", or "/subdirectory/something, will be redirected, while requests for "/anything-other-than-subdirectory" will be left alone.

Note that I also made the "www." optional and removed the end anchor from your hostname test -- again to avoid invoking both this rule and your main domain-canonicalization rule for the same client request.

If you haven't seen it already, this thread [webmasterworld.com] in our Apache Forum Library may save you a lot of aggravation -- and time, lost search rankings, and money as well.

Jim

JohnRuskin

9:53 pm on Jan 5, 2011 (gmt 0)

10+ Year Member



Thanks Jim:

I attempted your variant of the rule, for the browser entry:
www.mydomain.com/subdirectory
[note: NO trailing slash]

...your variant is just not executed. at all. placing it within the .htaccess in the ROOT or in the SUBDIRECTORY makes no difference. Instead, the follow on rule, originally quoted above in my original post as "1st Entry" DOES get executed, and it is that rule which exposes the directory path.

Note that the 1st Entry Rule, located in the SUBDIRECTORY's .htaccess:
## [1st entry] Move references [subfolder on main domain] to subdomain
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com$
RewriteRule ^(subdirectory/)?(.+)$ http://subdomain.mydomain.com/$2 [R=301,L]

expects a trailing slash in the condition, and is executed even though the browser request comes in without it....? And...despite that, it gets executed.

To me, it looks like the "
(.+)
", being empty, produces the exposed directory structure for the "$2", even though I would expect the $2 to also be empty/null.

I have looked at the post on ordering, you suggested; though valuable guidance, I'm not sure that it's instruction provides a clue for the resolution of the quirk I've run into.

I am also confused about how you mean multiple rule issue. I thought that once a rule is executed, through to the R=301, no other rules are executed, and the redirect control is handed back to the client browser, which then makes another request, which is then parsed by the module[s]. If the ROOT makes a 301, why would anything beyond that in the ROOT or in the SUBDIRECTORY get executed before the client/browser intervenes?

Thanks for your time...

John

JohnRuskin

9:57 pm on Jan 5, 2011 (gmt 0)

10+ Year Member



I should point out that my .htaccess files [in ROOT and in SUBDIR] do not do any internal rewrites, rather they all are external redirects.

I have no idea what the server config file does, however, and couldn't say that they are/aren't fiddling with internal rewrites ...

jdMorgan

11:50 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is this rule intended to 'fix up' a problem with what is called a "control-panel add-on domain"?

If so, I mistook the scenario...

Jim

JohnRuskin

6:35 am on Jan 6, 2011 (gmt 0)

10+ Year Member



no...this was simply a hunt to correct that weird case where the / was missing in that URL i mentioned.

the balance of problems had been solved [thanks to your kind help/suggestions].