Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 which I want to transform to

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]


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?

share|improve this question
Can you clarify what you're looking for with "directory agnostic"? An htaccess file in the same location as the web application will always apply to it (with an implicit RewriteBase of the directory that the htaccess is placed in), or mod_rewrite config in the main Apache config in a VirtualHost block. Which part of the config needs to be able to change - the install directory, or the URL path? – Shane Madden Nov 12 '11 at 19:58
Hi Shane: The install directory because I'd like end users to be able to install the application in whatever dir they like, so it could be /var/www/html/ or /home/user/public_html but obviously the RewriteBase would need to work for both. I've tried using RewriteBase / which works if the application is in /var/www/html/ but not when placed in /home/user/public_html. – Bendihossan Nov 12 '11 at 20:06

For the use cases that you're talking about, having no explicit RewriteBase (and just letting it use current directory context, as is the default) should work just fine.

As for the ID conversion, something along these lines should work:

RewriteRule ^customers/(\d+)$ customers.php?ID=$1 [L]
share|improve this answer
Thanks for the answer, but if I have no RewriteBase I get The requested URL /~user/webapp/customers was not found on this server. when I visit Permissions are correct because if I add .php to the end the script runs OK. – Bendihossan Nov 13 '11 at 15:43

Try this:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule /~user/webapp/customers/([0-9]+)$ /~user/webapp/customers.php?ID=$1 [QSA,L]

This should work!

(PS: I try to avoid RewriteBase because (believe my experience) you may get into trouble if yo have a lot of rules and you edit them many months later (= you don't remember there's a RewriteBase))

share|improve this answer
up vote 0 down vote accepted

After digging around I was able to answer my own question. When the webapp is installed it asks for a web address and a local file path for the app and uses that in the .htaccess as it's RewriteBase.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.