Forum Moderators: phranque

Message Too Old, No Replies

Adding Trailing Slash and Redirect Problem

         

jmblock2

2:31 pm on Aug 14, 2010 (gmt 0)

10+ Year Member



Hi, I've been searching through a bunch of threads on these issues and have tried out code that has been listed and am still just stuck. So I though I would try and get some feedback on what I am doing wrong.

My setup is, I have domain.com and a root folder at /home/user/

I'm planning on having several sites so I am making individual domain folders, /home/user/domain.com/

In my .htaccess in /home/user/ I have:
Options +FollowSymLinks -Indexes +MultiViews
DirectoryIndex index.php index.xhtml index.html
DirectorySlash On
RewriteEngine On

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

#add trailing slash for directories
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://domain.com/$1/ [R=301,L]

#Move default directory of domain.com from / to /domain.com/
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/domain.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ domain.com/$1 [L]

RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(/)?$ domain.com/index.php [L]


I added a wordpress under /home/user/domain.com/blog/
and two pages /home/user/domain.com/index.php and /home/user/domain.com/site/index.html

So here are my test cases and their outputs:
[domain.com...] -> [domain.com...] - correct
[domain.com...] -> [domain.com...] - why is it showing the folder structure?
[domain.com...] -> [domain.com...] - correct
[domain.com...] -> [domain.com...] - why does this get stuck in a loop and then 404 out?
[domain.com...] -> [domain.com...] - correct

So after reading a few posts I wanted to find out:
1)are my rules are correct for the task they suggest?
2)is the order of the rules sequentially correct?
3)are my modifiers and use of [R=301] and [L] correct?
4)Since there are no other .htaccess in the system except for this one, why does the mapping of /blog and /site differ so much? It seems like they should both have the same problem.
5) Why is the folder structure displayed for both?
6) Why does /blog go into an infinite loop?

Also, what is the difference between a redirect and a rewrite? Is a redirect with [R] and rewrite with [L]?

Sorry if the post is lengthy, I am just trying to be thorough because I am have been struggling with this for quite a few hours. Thanks for your time and I look forward to any suggestions!

Jacob

jmblock2

4:33 pm on Aug 14, 2010 (gmt 0)

10+ Year Member



I've been working on it some more and this I've found that, in the adding of the / to directories, if I do this:
RewriteCond %{REQUEST_URI} !.*/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://domain.com/$1/ [R=301,L]

It hides the /domain.com/ from showing in the URL, but then it appends a / to my css files, such as "/blog/wp-content/themes/rapid/style.css"

If I do this:
RewriteCond %{REQUEST_URI} !.*/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ http://domain.com/$1/ [R=301,L]

It shows the /domain.com/ in the URL, but the CSS comes through okay, it doesn't append a / to the end.

So why doesn't the -f stop the .css file from being appended a /? Does it have something to do with URI path vs physical path? Are !-f and -d not the same?

Also, am I supposed to be using a second .htaccess in the folder /domain.com/ ? I've messed with setting RewriteBase /domain.com but it hasn't gotten me any closer.

Thanks again,

Jacob

jdMorgan

2:30 am on Aug 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No time for long or well-organized answers right now, but three points:

%{REQUEST_FILENAME} !-f "URL does not resolve to a physically-existing file"
%{REQUEST_FILENAME} -d "URL resolves to a physically-existing directory"

So, no they're not the same.

Your first two rules are not in the correct order. External URL-to-URL redirects first, in order from most-specific (fewest URLs affected) to least-specific, followed by internal URL-to-filepath rewrites, again in order from most- to least-specific.

If you are adding more than one or two "domains", you will be much happier if you use a consistent filestructure, and 'tag' all separate-domain filepaths by organizing them like

/home/users/domains/domain.com
/home/users/domains/domain2.com
/home/users/domains/domain3.oom

In this way, looping can be prevented by using a simple check to see if the path has already been rewritten and starts with "domains/". File and directory "exists" checking is very inefficient (an may beay your hard drive to death) and should be avoided whenever possible.

I'm not sure where your blog fits in, and can't comment on its looping problem because I see no code for it and its URL and filesystem locations are not clear.

Use Apache mod_dir to automatically add missing trailing slashes when needed.

Be aware that if any rule is invoked, mod_rewrite processing in .htaccess will be re-started. You must therfore be quite sure that you rules are mutually-exclusive in order to prevent looping.

Currently, with many of your %{REQUEST_FILENAME} checks failing due to looking at the wrong (i.e. not yet rewritten) filepaths, I suspect your add-a-slash rule is redirecting previously-rewritten filepaths, and exposing the filepaths.

Seriously, get rid of all of the exists checks by using more specific URL-based testing. For example, add a slash if the current URL-path is non-blank and the final path-part does not contain a period: ^(([^/]+/)*[^.]+)$

Jim