0

I noticed that adding a trailing / to index.php breaks css and javascript which was explained here- what-happens-when-i-put-a-slash-after-a-php-url

My rewrite rule takes whatever string is after the domain and a forward slash and puts it into the GET variable q. So foo.com/foo works fine and I can access /foo in the GET variable q. How do I get any non existent resource requested in url string to work similarly? Make foo.com/foo/ OR foo.com/foo/foo etc. redirect to index.php and not break css and javascript.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./index.php?q=$1 [NC,L,QSA]
</IfModule>

My rewrite rule works when a query string is appended to index.php or when /index.php is replaced with /foo but breaks js and css when additional directories are added like /foo/ or /foo/foo. How does the rule need to be written to prevent this?

2
  • put a example of yours css or js files' url Commented Mar 15, 2020 at 23:11
  • <script type="text/javascript" src="js/search.js"></script> <link rel="stylesheet" type="text/css" href="css/styles.css"/> Commented Mar 15, 2020 at 23:15

1 Answer 1

1

It appears that you use relative links for including your javascript or css files. So, all you have to do is make your file addresses absolute, like /js/search.js. Your rewrite rule is correct and it won't forward the actual files to your index.php file. But when you say src='js/search.js', it means index.php/js/search.js for the browser, and that is not an actual file address.

Sign up to request clarification or add additional context in comments.

1 Comment

Instead of changing relative paths to absolute, I found the answer in a similar question by adding this to the head <base href="localhost/Foo" /> Now everything works as expected. Thanks!

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.