0

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?

2
  • 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? Nov 12, 2011 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. Nov 12, 2011 at 20:06

3 Answers 3

0

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]
1
  • 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. Nov 13, 2011 at 15:43
0

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))

0

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .