Search code examples
phpapache.htaccessmod-rewritemod-userdir

.htaccess routing in domain ip with ~user parameter (apache UserDir)


I've developed an application in PHP using a routing system through .htaccess and until today, I've managed to deploy this application in any server environment without any problems.

Today, a client of mine asked me to deploy the application to it's server that doesn't have any domain associated yet, and for that reason, he provided me the IP address for me to test if everything was ok. The problem is that this IP requires a username parameter to be present in the URL and that is causing the execution to always be redirected to the servers default 404 error page.

The IP is something like this (some values have been altered):

http://192.185.65.31/~gef/

If I execute just that link without any additional parameters, I can reach my serverindex.php file that is configured to be my DirectoryIndex in .htaccess. But if I try to add anymore parameters like the language for example (en/), I'm automatically redirected to the 404 page.

My .htaccess routing configuration is as follows:

DirectoryIndex serverindex.php

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . serverindex.php

This configuration always allowed me to receive and manipulate an URL like the example bellowe:

Example: http://example.com/param1/param2/param3

Array
(
    [0] => param1
    [1] => param2
    [2] => param3
)

How can I set .htaccess so that I leave the username untouched in the URL (so the server can process it and log the correct user account), and pass the following data as parameters into the application?

Example: http://example.com/~username/p1/p2/p3

Array
(
    [0] => p1
    [1] => p2
    [2] => p3
)

Solution

  • You have a relative path for your target script. You might need to add a RewriteBase for this to work, e.g.

    RewriteBase /~gef/