I am adding a new application to my site at example.com/app. I want views at that URL to always open myapp.php.


example.com/app -> example.com/app/myapp.php and
example.com/app/ -> example.com/app/myapp.php

What's the correct form of rewrite rules in the .htaccess file?

I've got:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /app/
RewriteRule ^myapp\.php$ - [L]
RewriteRule ^myapp.php$ - [L]
RewriteRule . - [L]

...based on what the Wordpress front-end does. But all I see at example.com/app is a directory of files. :(

(I put those rewrites at the top of my .htaccess file).

Any ideas?

Update What actually worked:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app(/.*)?$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /app/myapp.php [L]

This is good because:

  1. Explicit or implicit calls to app/myapp.php work.
  2. example.com/app redirects to app/myapp.php
  3. example.com/app/ redirects to app/myapp.php
  4. example.com/app/subfunction redirects to app/myapp.php
  5. All other calls to example.com/otherstuff are untouched.

Item 4 is Wordpress-like Front Controller pattern behaviour.

I think that rule

RewriteCond %{REQUEST_URI} ^/app.*$ [NC]

needs refining as it allows /app-oh-my-goodness etc. through too. Thanks for the answers.

2 Answers 2


DirectoryIndex sound more like what you want. (If mod_dir is installed)

DirectoryIndex myapp.php

Documentation: http://httpd.apache.org/docs/2.0/mod/mod_dir.html

Or if you still want mod_rewrite, this should work:

RewriteRule app(/*)$ http://example.com/app/myapp.php [L]
  • This is close but spoils needed access to myapp.php (and CSS files etc.) so needs RewriteCond line. See edited question. Also the rule is a little too wide I think. - Great call on "DirectoryIndex". I had forgotten that rule and I like it.
    – Dizzley
    Commented Feb 27, 2011 at 8:52
  • $ should keep it from matching css or php files. RewriteCond might not be necessary unless it becomes more complex.
    – J.Romero
    Commented Feb 27, 2011 at 9:04
  • Doesn't "/*" mean "0 or many /", and "/?" is a bit better? Still... thank you.
    – Dizzley
    Commented Feb 27, 2011 at 9:33


RewriteRule ^app/?$ app/myapp.php [L]
  • Thanks - that "?" means "0 or 1 /" - helps a lot. Need rules for app, app/ and app/subfunction really. What about "^app(/.*)?$" ?
    – Dizzley
    Commented Feb 27, 2011 at 9:16
  • Yes ? means 0 or 1 in this case. That looks correct. And you can use $1 to pass "subfunction" to thr url you are redirecting to.
    – Zimbabao
    Commented Feb 27, 2011 at 9:18
  • You win Best Answer but you are both awesome and helpful!
    – Dizzley
    Commented Feb 27, 2011 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.