Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

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
add comment

3 Answers

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 http://server.domain.com/~user/webapp/customers. Permissions are correct because if I add .php to the end the script runs OK. –  Bendihossan Nov 13 '11 at 15:43
add comment

Try this:

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

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
add comment
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
add comment

Your Answer

 
discard

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.