.htaccess rules to rewrite URLs to front end page?

Posted by Dizzley on Pro Webmasters See other posts from Pro Webmasters or by Dizzley
Published on 2011-02-27T07:54:17Z Indexed on 2011/02/27 15:32 UTC
Read the original article Hit count: 254

Filed under:
|

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.

© Pro Webmasters or respective owner

Related posts about htaccess

Related posts about rewrite