Forum Moderators: phranque

Message Too Old, No Replies

.htaccess subfolder redirect + missing trailing slash

         

Jakobud

6:35 am on Aug 8, 2009 (gmt 0)

10+ Year Member



I wanted to have my main domain be redirected to a subfolder:

/public_html/mydomainfolder/

There are lots of posts on this all over the net, but the one I found that works the best was one I found on here posted by jdMorgan:


# Redirect to subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteCond %{REQUEST_URI} !/mydomain.com/
RewriteRule (.*) /mydomainfolder/$1 [L]

This worked very well for almost everything. However one issue I can't figure out. When I attempt to access a subdirectory and DO NOT add the trailing slash, I see the 'mydomainfolder' in the url appear. Here is an example:

I type in this:

http://www.mydomain.com/randomsubfolder

and I get this:
http://www.mydomain.com/mydomainfolder/randomsubfolder/

However if I type it in WITH the trailing slash, I get the correct URL:

http://www.mydomain.com/randomsubfolder/

and I get this:
http://www.mydomain.com/randomsubfolder/

So the first thing I thought was to add a rewrite before the above code, that checks to see if the requested file was a directory and if so, add a slash to it, but that didn't seem to do anything.

Anyone got any clues on what to do here?

Jake

Caterham

11:05 am on Aug 8, 2009 (gmt 0)

10+ Year Member



You'll need a rule which fixes the trailing slash manually if you don't want to expose the real path.

Jakobud

3:19 pm on Aug 8, 2009 (gmt 0)

10+ Year Member



Yes I tried this. I found some code online that did this. I placed it before the above code but it didn't seem to work. Perhaps the code wasn't correct, I'm not sure.

Can someone help me with the necessary code?

Caterham

8:45 pm on Aug 8, 2009 (gmt 0)

10+ Year Member



I found some code online that did this. I placed it before the above code but it didn't seem to work. Perhaps the code wasn't correct, I'm not sure.

If you don't post it, we can't see what went wrong.

A simple one, which assumes that you don't use periods for paths that should get a trailing slash would look like

RewriteRule ^([^.]+[^/])$ http://example.com/$1/ [R=301,L]

g1smd

10:20 pm on Aug 8, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What code did you try?

By the way, the original code that you posted and called a "redirect" is not a redirect at all. It is a "rewrite".
Be very careful not to confuse these two terms. They are very different things.

Jakobud

4:42 am on Aug 9, 2009 (gmt 0)

10+ Year Member



Sorry about the miscommunication in what is what. Making up apache directives and regex is pretty new to me.

Okay I have been messing around some, and I think I almost got it all figured out. But I need a tad bit more help still

Here is my code for adding a slash to the end of directories:

RewriteEngine on 
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.mydomain.com/$1/ [L,R=301]

Okay so this worked after I struggled with it some more! However, one weird thing, if I type in the following:

http://www.mydomain.com/randomsubfolder/index.php

My url changes to:
http://www.mydomain.com/randomsubfolder/index.php/

It adds a slash to the end even though index.php is a file. Shouldn't my code ignore files because of the following line?

RewriteCond %{REQUEST_FILENAME} !-f

Also, a weird thing about this behavior, it only happens if I'm accessing a subfolder's file. If I try to type in:

http://www.mydomain.com/index.php

It doesn't add the slash. Only happens for subfolders.

Also, I realize that a simple workaround to this is that I can add the following condition to ignore anything with a dot (.) in it (as in, files):

RewriteCond %{REQUEST_URI} !\..+$

however I'm curious as to why my original condition that checks if the file is a file isn't working.

Any thoughts?

Thanks for the help guys. I really do appreciate it!

jdMorgan

5:00 am on Aug 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does /randomfolder/index.php exist, in that random folder? If not, then it will not be seen to exist by your rewritecond.

I'd also strongly suggest that you reverse your two rewriteconds, because file-exists checks are quite 'expensive' in terms of server resources. There is no use checking for file exists if the requested URL-path ends with a slash, so do that check first.

BTW, your "!(.*)/$" pattern can be more-efficiently and equivalently coded as "!/$" meaning "does not end with a slash."

We're going to need to see *all* of your rules, all at once, and in the order you've got them in your file to get much further with this. Your initially-reported problem is likely due to an interaction between your rules themselves, or possibly between mod_rewrite and mod_dir.

Jim

Caterham

2:12 pm on Aug 9, 2009 (gmt 0)

10+ Year Member



Also, I realize that a simple workaround to this is that I can add the following condition to ignore anything with a dot (.) in it (as in, files):

RewriteCond %{REQUEST_URI} !\..+$

In that case you can use the rule-pattern

^([^.]+[^/])$
w/o the condition(s) as above (conditions are checked after the rule-pattern)...

Jakobud

6:06 am on Aug 10, 2009 (gmt 0)

10+ Year Member



Okay thanks for the continued help guys. I'm looking forward to being able to fully understand this stuff. Here is my complete .htaccess:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /

#Add trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\..+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ [mydomain.com...] [L,R=301]

# redirect non-www to www
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ [mydomain.com...] [L,R=301]

# Redirect to subfolder
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteCond %{REQUEST_URI} !/domainfolder/
RewriteRule (.*) /domainfolder/$1 [L]

jdMorgan

7:09 pm on Aug 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since you didn't answer my question above, let's just get rid of the 'file-exists' dependency altogether, since in almost all cases, all HTTP-accessible files in your account will have an extension.

RewriteEngine on
Options +FollowSymlinks
RewriteBase /
#
# Externally redirect to add trailing slash to extensionless URLs if missing
RewriteRule ^(([^/]+/)*[^./]+)$ http://www.example.com/$1/ [L,R=301]
#
# Externally redirect non-www requests to www
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#
# Internally rewrite all requests to /domainfolder, unless already done
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com
RewriteCond %{REQUEST_URI} !/domainfolder/
RewriteRule ^(.*)$ domainfolder/$1 [L]

Jim

Jakobud

5:04 pm on Aug 11, 2009 (gmt 0)

10+ Year Member



Sorry I missed that question. Yes the index.php does indeed exist in that folder.

I'll try your method there, Jim. You guys sure do seem to know this stuff well!