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/topics/accordion_menu_not_compatible_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.

Moving to Apache Configuration for discussion on the .htaccess situation.

Could you also post your .htaccess please? Without that we can only guess :slight_smile:

Thanks PMW, and thanks for the quick reply ScallioXTX. :slight_smile:

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

[HIGHLIGHT=]

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...:confused:

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:


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 :slight_smile:

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.

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!?

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…

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.

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

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 :slight_smile:


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.


# 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[COLOR="Red"]/[/COLOR]
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


RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f

in there? What is that supposed to do?


# 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:


# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
[COLOR="Red"]RewriteCond %{REQUEST_URI} !(\\.)
RewriteCond %{REQUEST_URI} !(.*)/$[/COLOR]
RewriteRule ([^/]+)[COLOR="PaleGreen"][B]/[/B][/COLOR]$ $1/ [L]

Remove lines in red, add / in green


# 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 [noparse]yourdomain.com/app[/noparse] 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


# Rewrite any calls to /* or /app to the index.php file
[COLOR="Red"]RewriteCond %{REQUEST_URI} /app/$[/COLOR]
RewriteRule ^app[COLOR="Red"]/[/COLOR] index.php [L]

(again, remove red)


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.


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php?%{REQUEST_URI} [L]

Lastly,


# 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:


# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public[COLOR="Red"]/[/COLOR]
RewriteRule .? public/%{REQUEST_URI} [L]

:slight_smile:

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:



Options +FollowSymLinks
RewriteEngine On
[COLOR="Gray"]RewriteBase /[/COLOR]
# 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]
[indent]Changes directory level for relative links![/indent]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
[COLOR="Red"]RewriteCond %{REQUEST_FILENAME} !-d[/COLOR]
[indent]Seriously, if not a directory then treat like a directory?[/indent]
[COLOR="Red"]RewriteCond %{REQUEST_URI} ![COLOR="Gray"]([/COLOR]\\.[COLOR="Gray"])[/COLOR][/COLOR]
RewriteCond %{REQUEST_URI} ![COLOR="Gray"](.*)[/COLOR]/$
RewriteRule ([^/[COLOR="Blue"].[/COLOR]]+)$ $1/ [L]
[indent]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)?[/indent]
# Rewrite any calls to /* or /app to the index.php file
[COLOR="Red"]RewriteCond %{REQUEST_URI} /app/$[/COLOR]
RewriteRule ^app/ index.php [L]
[indent]That handles app/ without the RewriteCond.[/indent]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?$1 [L]
[indent]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?[/indent]
# 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

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!!
:x

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:

[HIGHLIGHT=""]
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.

[HIGHLIGHT=""]

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 !

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 [noparse]

...

[/noparse] encoding):


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]
[indent]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.[/indent]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
[COLOR="Red"]RewriteCond %{REQUEST_FILENAME} !-d[/COLOR]
RewriteRule ([^/]+)/$ $1/ [L]
[indent]You had this, too!  If it's not a directory then add
a trailing slash to the directory?[/indent]

# Rewrite any calls to /* or /app to the index.php file
RewriteRule ^app index.php [L]
[indent]Nothing about * - what is that supposed to be?[/indent]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php?%{REQUEST_URI} [L]
[indent]Technically, there should be a key to assign %{REQUEST_URI} value.[/indent]

# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ![COLOR="DeepSkyBlue"]^[/COLOR]public[COLOR="DeepSkyBlue"]/[/COLOR]
[indent]I'd add the [COLOR="DeepSkyBlue"]blue[/COLOR] to make "public" be a subdirectory of DocumentRoot.[/indent]
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:

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} ![B]^[/B]public[B]/[/B]
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\\.(html|json|xml|atom|rss|rdf|txt)$ $1[COLOR="Red"]/[/COLOR] [L]

# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ![COLOR="Red"](.*)[/COLOR]/$
# Not necessary - don't capture garbage!
[COLOR="Red"]RewriteCond %{REQUEST_URI} !(.*)/?[/COLOR]
# 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} ![B]^[/B]public/
RewriteRule .? public%{REQUEST_UTI} [L]

Regards,

DK