.htaccess and rewrite rules in PHP server

Asked

Viewed 223 times

2

I am developing a php microframwork, and I am using the built-in php server (php -S 127.0.0.1:8000 -t public) with root directory in the public folder/.

I don’t know if . htaccess works or is working on this server.

It happens that when accessing a non-root path ("/") such as "/properties", the path of CSS files, images and the like are overlapping the path, such as: "/properties/ext/css/...", where the normal should be: "/ext/css/...".

Accessing the route "/properties" (WORKING CORRECTLY):

[Sat Nov 17 20:35:12 2018] ::1:34630 [200]: /propriedades
[Sat Nov 17 20:35:12 2018] ::1:34634 [200]: /ext/css/core.css
[Sat Nov 17 20:35:12 2018] ::1:34636 [200]: /img/logo02.svg
[Sat Nov 17 20:35:12 2018] ::1:34638 [200]: /img/default-image.png

Accessing the route "/properties/" (PROBLEM):

[Sat Nov 17 20:35:10 2018] ::1:34620 [200]: /propriedades/
[Sat Nov 17 20:35:10 2018] ::1:34622 [404]: /propriedades/ext/css/core.css - No such file or directory
[Sat Nov 17 20:35:10 2018] ::1:34626 [404]: /propriedades/ext/js/core.js - No such file or directory
[Sat Nov 17 20:35:11 2018] ::1:34628 [404]: /propriedades/ext/js/core.js - No such file or directory

Routes are captured and handled by the route class of the system. Below follows the file . htaccess:

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

Can someone at least give me some idea of what I should try to solve?

1 answer

1


I don’t know if . htaccess works or is working on this server.

The internal php server will not consider . htaccess

Can someone at least give me some idea of what I should try to solve?

Regardless of . htaccess you need to fix how your HTML references the CSS files. You are probably using relative links:

<link rel="stylesheet" type="text/css" href="ext/css/core.css">

At the root of the site this works well, but when accessing another route, the browser sends along the url segment.

You can reference this file from the root, so it will work on any route:

<link rel="stylesheet" type="text/css" href="/ext/css/core.css">

It is also interesting to generate the full URL, and in the future allow it to be configurable for a different domain (using a CDN for example) to have more control:

<link rel="stylesheet" type="text/css" href="http://localhost/ext/css/core.css">

Browser other questions tagged

You are not signed in. Login or sign up in order to post.