1

I am using a small redirect to let my index.php resolve all parameters, but in some cases it can't find my javascript files anymore.

This small .htaccess rewrites all requests that don't exist to index.php.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)/$ index.php/$1

A piece of my index.php where the javascript url returns index.php instead:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <script type="text/javascript" src="scripts/jquery-1.10.2.min.js"></script>
  • www.mysite.com/foo is working fine
  • www.mysite.com is working as well
  • www.mysite.com/foo/foo isn't working. It returns index.php as a javascript file when I inspect it with firebug.

Why is the relative path changed? I am redirecting to index.php so I'm expecting to be in that folder.

As an alternative: How can I get the full path of the javascript that is being requested for debugging purposes? Is there a way to find the .js requests that are being made by index.php?

1 Answer 1

1

Because of the extra slash, the browser thinks that your relative links (e.g. "scripts/jquery-1.10.2.min.js") have a URI base in a subdirectory. So you either need to make your links absolute instead of relative:

<script type="text/javascript" src="/scripts/jquery-1.10.2.min.js"></script>

Or add a relative URI base in your header:

<base href="/" />

The other thing here is that your second rewrite rule doesn't have any conditions. RewriteConds only get applied to the immediately following RewriteRule. The other thing is that the first rule will always get applied because (.*) matches everything, including URI's with a trailing slash. Thus the second rule never gets applied. You should always make the slash optional:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ index.php/$1
Sign up to request clarification or add additional context in comments.

5 Comments

I can't quite understand why that is the case. If I'm redirecting to index.php I would expect that the scripts are requested as coming from the index.php folder.
@Marnix Your redirect is entirely on the server's end. The browser still thinks it's seeing the resource located at: www.mysite.com/foo/foo, which means the relative URI base is /foo/, and thus when it sees a relative URI, it appends the base that it thinks is correct to it.
But how would it be easy to move these folders around without changing the paths of each file every time you want to move something?
@Marnix You don't need to change where the scripts are, just tell the browser that /foo/ isn't the base, and that / is. You do that by adding / in front of the links, or simply adding a <base> tag to your header.
This seems to go right for all my cases I've showed in my question. But this case doesn't seem to work: www.mysite.com/foo/foo/?boo=hey. My $_SERVER['ORIG_PATH_INFO'] becomes /index.php/foo/foo/foo. That doesn't make sense to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.