Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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

E.g.

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]
</IfModule>

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

share|improve this question

migrated from stackoverflow.com Feb 27 '11 at 10:34

This question came from our site for professional and enthusiast programmers.

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]
share|improve this answer
    
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 Feb 27 '11 at 8:52
    
$ should keep it from matching css or php files. RewriteCond might not be necessary unless it becomes more complex. –  J.Romero Feb 27 '11 at 9:04
    
Doesn't "/*" mean "0 or many /", and "/?" is a bit better? Still... thank you. –  Dizzley Feb 27 '11 at 9:33

Try

RewriteRule ^app/?$ app/myapp.php [L]
share|improve this answer
    
Thanks - that "?" means "0 or 1 /" - helps a lot. Need rules for app, app/ and app/subfunction really. What about "^app(/.*)?$" ? –  Dizzley Feb 27 '11 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 Feb 27 '11 at 9:18
    
You win Best Answer but you are both awesome and helpful! –  Dizzley Feb 27 '11 at 10:14

Your Answer

 
discard

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