I'm in the process of developing a PHP application but I have limited working knowledge of mod_rewrite which I plan to use to create "pretty URLs".
Whilst the application is in development I'm working on it in my public_html
folder. At the moment I have an index.php
which provides access to other PHP files like customers.php
which takes an ID and looks for that customer's details in a DB and displays them.
The unprettified URL looks like http://server.domain.com/~user/webapp/customers.php?ID=1
which I want to transform to http://server.domain.com/~user/webapp/customers/1
.
I've written the following mod_rewrite rule which works to rewrite the webapp/customers.php
to webapp/customers/
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~user/webapp/
rewriterule ^customers$ customers.php [L]
</IfModule>
There is an obvious drawback to this (I'll worry about catering for the ID in the URI later for now). This is placed in a .htaccess and assumes that the application lives in /home/user/webapp/
on the server.
This is clearly limiting and very inflexible, so I want to know is there's a way of creating directory agnostic mod_rewrite rules so that it doesn't matter where the application has been installed? I realise it's probably never going to be perfect as it depends on the server configuration but there must be a better way of doing what I'd like than the above?
RewriteBase
of the directory that the htaccess is placed in), or mod_rewrite config in the main Apache config in aVirtualHost
block. Which part of the config needs to be able to change - the install directory, or the URL path?/var/www/html/
or/home/user/public_html
but obviously theRewriteBase
would need to work for both. I've tried usingRewriteBase /
which works if the application is in/var/www/html/
but not when placed in/home/user/public_html
.