Forum Moderators: phranque
/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 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/ 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
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]
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 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!
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
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)...
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]
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]