how to remove php extension in .htaccess
example media.php?page=test

Well have you read any .htaccess tutorials yet?

already but have not been able to help me give you the example of

Well, the .htaccess that I use is usually something like:

RewriteEngine on

Options -Indexes

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

This redirects everything to index.php. You can then use PHP to read the query string ($_SERVER['REQUEST_URI'] and other $_SERVER vars) and include the appropriate files. This .htaccess setup works like a charm for me.

I think for your specific case something like RewriteRule media\?.* media.php?$1 should also do the trick. But then again, the last time I wrote a .htaccess file is years ago so I cannot be so sure :p.

You need to define what you want to redirect from as well as what you want to redirect to. Assuming that you want to go from:

http://example.com/test

to:

http://example.com/media.php?page=test

Then this should do the trick:

<IfModule mod_rewrite.c>
    # Turn on the engine
    RewriteEngine On

    # If your pages are under the root of the domain, leave as /
    # If they are instead under a subdirectory such as /test/,
    # change RewriteBase to be /test/
    RewriteBase /

    # Don't redirect real files (-f) or directories (-d)
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    # Do the redirect - if you had to change RewriteBase, add that
    # here as well - /test/media.php instead of /media.php
    RewriteRule ^(.*)$ /media.php?page=$1 [L]
</IfModule>

But if your situation is more complex, this won't cut it:

  • Do some URLs need to be redirected to files other than media.php?
  • Are there multiple base directories to redirect from (not just / or /test/ as in my comments, but more than one base)?
  • Do you need query strings to still work? (as is, in the URL http://example.com/test?q=1 the q=1 value will be stripped out and won't be visible to media.php)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.