Forum Moderators: phranque
I've been searching around the web all day looking for an answer to my problem. I just joined this forum, so excuse any newbie mistakes.
I'm working on a site where I want to mask the .php extension on the files. I successfully turned on MultiViews to do this.
site.com/product.php
becomes
site.com/product
The next problem is that the product.php file is useless without parameters. It is just a shell for showing items from the database. In order for a visitor to see anything the URL must have something like ?ID=123 at the end of it.
I want site.com/product to redirect to a page at site.com/products (note the plural) ONLY if someone tries to type in the former URL with no parameter.
With the line
RedirectMatch permanent database databases
in my .htaccess file, I get a 404 Not Found error on site.com/product - which makes sense because that file isn't actually there, but MultiViews should know to interpret it as site.com/products.php right?
So ultimately this is what I'd like:
site.com/product?ID=123 to continue working (as it currently is with MultiViews enabled)
site.com/product to redirect to site.com/products (both of which are .php files in actuality)
Any ideas?
Thanks so much,
Tommy
This is usually only a matter of rewriting extensionless URLs to add a file extension if the resulting URL resolves to a file that exists when that extension is added. Although this may sound difficult, it's actually only a two-line mod_rewrite rule.
For example:
# If extensionless page URL with ".php" added resolves to an existing file
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
# rewrite extensionless page URL to .php file
RewriteRule ^(([^/]+/)*[^./]+)$ /$1.php [L]
Jim