Jump to content

Problem with JS plugins when htaccess rule is apllied


jimfog

Recommended Posts

The head section in one of my webpages has links to various JS files/plugins:

<script src="js/jquery-1.11.1.min.js" ></script> 
<script src="js/jquery.validate.js" ></script>
 <script src="js/jquery-ui 1.11.2.js"></script>
 <script src="js/jquery.bootpag.min.js" ></script> 
<script src="js/footer.js" ></script>
 <script src="js/html-inspector.js" ></script>

All the above in the following page:http://localhost/Appointments/Frontend/blog_show.php

 

In my htaccess file I have this rule:

RewriteRule ^blog_show/(.*)$ blog_show.php/?t=$1 [NC,L]

So rewriting takes place when the user types this in the web address bar:

http://localhost/Appointments/Frontend/blog_show/2016-07-28/

blog_show.php is called again but with a query string and here the problem appears.
The js files/plugins do not load correctly...the console emit this error:
I do not have the slightest idea why this happens...
Link to comment
Share on other sites

If you dig down in those error messages and look at the response for those Javascript files, I bet you'll find that your rewrite rule is taking the URLs for the Javascript files and sending it to the PHP script, which is outputting some HTML. Either move the Javascript files out of the path affected by the rewrite rule, or change the rule to exclude the Javascript files (and images, and CSS, and whatever else).

 

You should probably just use absolute URLs for any resources like Javascript files.

Link to comment
Share on other sites

In your htaccess file, always make sure the URL is not of an existing file or directory.

 

You would add RewriteCond statements before your RewriteRule statement.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Link to comment
Share on other sites

 

In your htaccess file, always make sure the URL is not of an existing file or directory.

 

You would add RewriteCond statements before your RewriteRule statement.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Ι do not understand.

First of all what these RewriteCond statements do?

 

You also say that the URL is not of an existing file/dir.

If that is the case how the rewrite rule must be....if not of an existing file/dir?

Cause you say also that there must be a rewriteRule statement.

Link to comment
Share on other sites

You can use the same RewriteRule directive you're currently using, but by preceding it with those RewriteCond directives you'll make sure it doesn't apply the rule to URLs that point directly at existing files or folders on the site.

Link to comment
Share on other sites

You can use the same RewriteRule directive you're currently using, but by preceding it with those RewriteCond directives you'll make sure it doesn't apply the rule to URLs that point directly at existing files or folders on the site.

I have come to understand what the problem is.This is my htaccess now...with all the changes suggested:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog_show/(.*)$ blog_show.php/?t=$1 [NC,L]

The problem persists though...

Link to comment
Share on other sites

What happens when you open the URL to the Javascript file directly in your browser?

 

It seems you're using relative URLs ( "js/jquery-1.11.1.min.js" ), your script tags should be relative to the site root so that they will open no matter what section of the site you're on. You can do that by prepending a slash to the URL ("/js/jquery-1.11.1.min.js")

Link to comment
Share on other sites

What happens when you open the URL to the Javascript file directly in your browser?

 

It seems you're using relative URLs ( "js/jquery-1.11.1.min.js" ), your script tags should be relative to the site root so that they will open no matter what section of the site you're on. You can do that by prepending a slash to the URL ("/js/jquery-1.11.1.min.js")

yes I am using relative URL's...I tried prepending a slash,as you said but for all the JS files I get 404 in the console.

Link to comment
Share on other sites

Well, the / indicates the location of the file relative to the site root. You need to put the entire path after the slash. I don't know your site's directory structure so I can't tell you what the URL should be.

Link to comment
Share on other sites

Well, the / indicates the location of the file relative to the site root. You need to put the entire path after the slash. I don't know your site's directory structure so I can't tell you what the URL should be.

Τhe folder structure is like this C:\Apache24\htdocs\Appointments\Frontend\js for JS files.

entering the full path would be a problem cause if I put in the script tag this <script src="Frontend/js/jquery-1.11.1.min.js" ></script>

the browser enters the full path itself...so the end result is this:

http://localhost/Appointments/Frontend/Frontend/js/jquery-1.11.1.min.js which leads of course to a faled request...

 

Anyway...what are you trying to achieve with this?

 

I have clearly identified the problem here with the links but I cannot find what is causing it.

 

After the rewrite rule is implemented take a look what the console shows for every JS file.

https://onedrive.live.com/redir?resid=BE27434B2AAC8130!456&authkey=!ABzaukrkGCA9W80&v=3&ithint=photo%2cPNG

 

ANYWAY...i think the solution is to change from relative to absolute URLs....I am just stuck in the implementation of it,it should be easy.

 

P.S I am hours in front of the PC and I am tired

Edited by jimfog
Link to comment
Share on other sites

Like I said, prepend a slash to the path to make it relative to the site root.

 

<script src="/Appointments/Frontend/js/jquery-1.11.1.min.js"></script>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...