SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy javascript not loading on .htaccess?

    Hey all,
    I feel terrible asking such a newbie question- but it has been such a quest building my portfolio website myself.

    I am using a simple accordion menu script on a php site based on the Stacey app. The issue is when ever I run things normally- everything is fine, as soon as I rewrite htaccess as .htaccess the javascript menu goes haywire....

    I have a thread up on their site already with a link to the live site- don't wanna repost my link:

    http://getsatisfaction.com/stacey/to..._with_htaccess

    I have everything posted there.... I'm pretty sure it has something to do with jquery and .htaccess rewrites- guessing this because the site works fine until rewrite to .htaccess, and even then if I manually add a '/?' to the end of 'localhost' or 'domain.com' it will work. This is where I lack knowledge and don't really even know what to search for!

    Note:

    after tinkering in some more on chrome's element inspector, I found that with clean urls enabled the page comes back with three errors:



    but reads the 4th javascript no problem-




    Hope someone is able to help.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Moving to Apache Configuration for discussion on the .htaccess situation.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Could you also post your .htaccess please? Without that we can only guess
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks PMW, and thanks for the quick reply ScallioXTX.

    my .htaccess file is as follows- keep in mind I'm a complete newbie to .htaccess probs and this is just my personal website!

    Code :
     
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
     
    ErrorDocument 404 /404.html
     
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
     
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.)
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ([^/]+)$ $1/ [L]
     
    # Rewrite any calls to /* or /app to the index.php file
    RewriteCond %{REQUEST_URI} /app/$
    RewriteRule ^app/ index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ index.php?$1 [L]
     
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !public/
    RewriteRule ^(.+)$ public/$1 [L]

    I just find it curious that it loads one .js and not the others...

  5. #5
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    I don't see anything in that .htaccess that would cause a 404 for those files. Are you 100% sure the files exist? Also check if the case is correct (i.e. the file is really called jquery-1.3.2.js and not jQuery-1.3.2 for example).

    From what I can gather from the .htaccess file, you have two directories, app/ and public/ and you want people to see the public/ directory and don't have access to the app/ directory, correct?

    If so, I would move everything from the public directory to the root directory of the site and place an .htaccess with the following in the app/ directory:

    Code:
    deny from all
    That way people also don't have access to the app/ directory (they get a "403 forbiden" if they try) and you don't have to have public/ in the URL each and every time, which makes the URL look cleaner. Also, if you do that you can throw about half of your current .htaccess out the window because it's needed anymore.
    Just let me know if what I'm assuming here is indeed the case and I'll walk you through how to amend the current .htaccess if you want
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  6. #6
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm a 100% that the .js files exist in both local and mydomain.com's public/docs/js- with correct spelling.. they are side by side with the gallery.js (which doesn't cause any errors when I rewrite to .htaccess).
    I thought it might be the dashes '-' in the name since the file without a dash wasn't having any probs... so I renamed the three .js files without a dash but still have the same issue.

    When I moved all the public files (.js and .css files) to root and added 'deny from all' to app/ it just gave me a forbidden error when I open index.

    what you're assuming is correct- if I'm understanding you correctly!
    all that app/ contains all of the .php files and public/ contains is .css and .js files that should be accessible by visitors browser as it contains required info for the site to work/look properly.

    Like I said... none of the errors occure under regualr htaccess!! Only when I rename to .htaccess.... even when it is htaccess I don't have public/ in my URL- just have a ? mark.... as in 'localhost/?/personal/residence'.... when I enable .htaccess it changes it to 'localhost/personal/residence'..... and that bloody ? is causing all of the problems!!

    If you could help me amend the current .htaccess maybe it will solve this issue.
    And for that I would forever be greatful, kind sir.

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    ramaLAMA, did you change anything? Those files load perfectly here (no 404).
    Do you have any add-ons (like adblock) in your chrome that might be screwing things up!?
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have Ad-block disabled- either way I have the same issue in Safari and Firefox.

    Didn't change anything...
    No 404 on the actual website right now because it is still htaccess not .htaccess....
    (hence the ? after mydomain.com)

    I'm trying to resolve it on localhost currently and getting 404 on those scripts....

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by ramaLAMA View Post
    I'm trying to resolve it on localhost currently and getting 404 on those scripts....
    Can you figure out which part of .htaccess is causing the problem, by commenting out all parts of it, and gradually re-introducing more and more of it until you experience the problem.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will do that.. hold on....

    Also note:
    when I rewrite to .htaccess there is no problems (no 404 on any script) IF I don't have menu-collapsed.js

  11. #11
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,039
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Ah, that explains why I wasn't getting 404s, I was testing on your live website.
    In the meantime, allow me to comment on your .htaccess

    Code:
    RewriteBase /
    RewriteBase is only needed to undo any effects of Redirect* directives. Since you don't have any of those, you can (and should) remove this line.

    Code:
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
    Remove the / in red to make the test a bit broader. Also, why is the
    Code:
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    in there? What is that supposed to do?

    Code:
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.)
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ([^/]+)$ $1/ [L]
    That block is kinda clumsy to be honest. If it's not a file and not a directory, who cares if it contains a dot? I would remove that line.
    Also, the RewriteCond can be incorporated in the RewriteRule by ending that one in a /
    Thus:
    Code:
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.)
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteRule ([^/]+)/$ $1/ [L]
    Remove lines in red, add / in green

    Code:
    # Rewrite any calls to /* or /app to the index.php file
    RewriteCond %{REQUEST_URI} /app/$
    RewriteRule ^app/ index.php [L]
    You don't need app in both the RewriteCond and the RewriteRule, that's superfluous. Plus, I would remove the / at the end of ^app/ in the rule (in case someone browses to yourdomain.com/app without the trailing slash. Okay, the trailing slash is added by the rule above so it will always be there, but just in case you ever remove the slash-adding rule and forget to amend this one I would do it anyway
    Code:
    # Rewrite any calls to /* or /app to the index.php file
    RewriteCond %{REQUEST_URI} /app/$
    RewriteRule ^app/ index.php [L]
    (again, remove red)

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ index.php?$1 [L]
    This works, but there is a more effecient way that doesn't need to create backreferences and is thus slightly faster.

    Code:
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .? index.php?%{REQUEST_URI} [L]
    Lastly,
    Code:
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !public/
    RewriteRule ^(.+)$ public/$1 [L]
    Same reasoning as the block above:

    Code:
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !public/
    RewriteRule .? public/%{REQUEST_URI} [L]
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  12. #12
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    rL,

    Sorry, I thought I'd answered this before your post:

    Have a look at the "Relative Links Are Missing!" section of my signature's tutorial article. Very clearly, this is the root of your problem.

    Code review:
    Code:
    Options +FollowSymLinks
    RewriteEngine On
    RewriteBase /
    # intent is to UNDO mod_alias Redirects - not needed and confuses things
    
    ErrorDocument 404 /404.html
    
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
    
    Changes directory level for relative links!
    # Add a trailing slash to directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
    Seriously, if not a directory then treat like a directory?
    RewriteCond %{REQUEST_URI} !(\.) RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ([^/.]+)$ $1/ [L]
    WHY create a whole new RewriteCond when you could merely include the dot in the RewriteRule (for efficiency as RewriteCond's are not even read if the RewriteRule doesn't match)?
    # Rewrite any calls to /* or /app to the index.php file RewriteCond %{REQUEST_URI} /app/$ RewriteRule ^app/ index.php [L]
    That handles app/ without the RewriteCond.
    RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ index.php?$1 [L]
    Not only does this change directory level but it also looks like a MultiViews call because you're prohibited directories but made this LOOK like a directory. What's the purpose of this?
    # Rewrite any file calls to the public directory RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !public/ RewriteRule ^(.+)$ public/$1 [L] [indent]If the request is not a file or directory and is not in the public directory, request it from the public directory? This sounds VERY dangerous as it will redirect EVERYTHING (which is not specifically omitted, i.e., files and directories).
    The answer to your question is in the change of directory levels. My question is WHY go through all this "nonsense" (okay, my personal biases shining through again - SORRY!) as the reasoning you've gone through to create it can't be guessed at?

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  13. #13
    SitePoint Member
    Join Date
    Jan 2011
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Red face

    well I read through the "Relative links are missing!!!"- btw very helpful link! Thanks a million!!
    But something was a miss because i have been using relative links... so I decided to try something and make all of the .js ref links "@root_path" links and voila!!



    After updating links on live site, which has no error 400s on any .js!
    I then turned my attention to .htaccess-

    ScallioXTX's advice landed me at a an error 500,
    with the .htaccess as follows:

    Code :
    RewriteEngine on
     
     
    ErrorDocument 404 /404.html
     
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
     
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ([^/]+)/$ $1/ [L]
     
    # Rewrite any calls to /* or /app to the index.php file
    RewriteRule ^app index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule .? index.php?%{REQUEST_URI} [L]
     
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !public
    RewriteRule ^(.+)$ public/$1 [L]

    I then implemented the revisions suggested by dklynn.

    Code :
     
    RewriteEngine on
     
    ErrorDocument 404 /404.html
     
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
     
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    RewriteCond %{REQUEST_URI} !(.*)/?
    RewriteRule ([^/]+)$ $1/ [L]
     
    # Rewrite any calls to /* or /app to the index.php file
     
    RewriteRule ^app/ index.php [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ index.php?$1 [L]
     
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !public/
    RewriteRule ^(.+)$ public/$1 [L]

    There was no server error. I'm very grateful for all the advice and help!!
    I am currently researching some of the other comments made by dklynn-
    in the meantime if you have any other revisions to .htaccess I'll gladly test and implement those also !

  14. #14
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    rL,

    Thanks!

    The problem is in using relative links but from different directory levels is that the browser, unless it's told otherwise (by the <base> tag), will look for relative links relative to the location specified in the location box. If that's changed by your redirection, then the relative link changes.

    As for ScallioXTX's code (please use [code]...[/code] encoding):
    Code:
    RewriteEngine on
    
    
    ErrorDocument 404 /404.html
    
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !public
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
    
    I think this was something you had but I loathe the addition of the trailing / (Missing Support Files problem) AND it will require Options +MultiViews which is a MAJOR PITA, IMHO.
    # Add a trailing slash to directories RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ([^/]+)/$ $1/ [L]
    You had this, too! If it's not a directory then add a trailing slash to the directory?
    # Rewrite any calls to /* or /app to the index.php file RewriteRule ^app index.php [L]
    Nothing about * - what is that supposed to be?
    RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .? index.php?%{REQUEST_URI} [L]
    Technically, there should be a key to assign %{REQUEST_URI} value.
    # Rewrite any file calls to the public directory RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !^public/
    I'd add the blue to make "public" be a subdirectory of DocumentRoot.
    RewriteRule ^(.+)$ public/$1 [L]
    A 500 error is generally caused by a syntax error and I saw nothing in there that would cause that error.

    Since I did not go through your code in that much detail, please take the comments above as applying to the code I'd presented, too:
    Code:
    RewriteEngine on
    
    ErrorDocument 404 /404.html
    
    # Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^public/
    RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
    RewriteRule (.+)\.(html|json|xml|atom|rss|rdf|txt)$ $1/ [L]
    
    # Add a trailing slash to directories
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !(.*)/$
    # Not necessary - don't capture garbage!
    RewriteCond %{REQUEST_URI} !(.*)/?
    # does not contain a / or does not contain an optional /  ???
    RewriteRule ([^/]+)$ $1/ [L]
    # Why not just check -d and then add a trailing / if it's not already present?
    
    # Rewrite any calls to /* or /app to the index.php file
    
    RewriteRule ^app/ index.php [L]
    # Again, what is *?
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)/$ index.php?$1 [L]
    # All that just to strip a trailing /?  :nono:
    # WHY add it in the first place?  :kaioken:
    
    # Rewrite any file calls to the public directory
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^public/
    RewriteRule .? public%{REQUEST_UTI} [L]
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •