0

Possible Duplicate:
Remove .PHP File Extension in URL

I have my .htaccess file set up to omit .php extensions from URLs. For instance:

mysite.com/page
mysite.com/item
mysite.com/about

Read from:

mysite.com/page.php
mysite.com/item.php
mysite.com/about.php

Which is great; however, I use the remaining part of the URL for parameters, which I want to be ignored. For instance, I want:

mysite.com/item/1234567890/a-product

To read from:

mysite.com/item.php

Rather than:

mysite.com/item/1234567890/a-product.php

Any idea how I can accomplish this in .htaccess? Here is my current .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Community
  • 1
  • 1

2 Answers2

0

You could use some sort of "router" to route all requests through a router file. In that router file you could sniff out the rest of the url and do with it what you want.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ router.php?url=$1 [PT,L,QSA]
</IfModule>

Then inside router.php, something like:

if (isset($_GET["url"])) {
    $url = $_GET["url"];
}

/* separate $url into variables, include your item.php file, pass rest of $url to included file */

Not sure if that's the correct way to do it, but it's one way.

owenconti
  • 400
  • 3
  • 6
0

Try:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/([^/]+)/?
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.*)$ %1.php [L]

This does what your rules originally did as well as remove the stuff after the first folder.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220