Search code examples
.htaccesssubdirectory

Adding a subdirectory to an invisible .htaccess URL rewrite


I wanted to add a subdirectory to my url so it would become easier to read:

Example of what i'd like:
localhost/testwebsite/users.php?firstname=john
should become
localhost/testwebsite/users/john

I tried using the .htaccess mod_rewrite rules and came up with this:

RewriteEngine On
RewriteBase /testwebsite/
RewriteRule ^users/(.*)$ users.php?firstname=$1

What happens why I use that code: it redirects the page successfully, it shows the html of the correct user and it processes the argument correctly. However, all stylesheets, images, scripts, anything with a relative path, could not be found and respond with a 404 message, because of the extra subdirectory added in the new url, I reckon.

Am I doing something wrong? Is there another technique I should be using? Or should I simply make all paths in my project absolute with regards to the root?


Solution

  • You're doing it right. The browser doesn't know that the actual path of the file is different. Use absolute paths or make paths relative to the easier to read URL.