Rewrite All Queries to Not Need the .PHP Extension Using a Mod_Rewrite Rewriterule

Rewrite all queries to not need the .php extension using a mod_rewrite RewriteRule

I would do it this way. Basically, if file doesn't exist, try adding .php to it.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ $1.php [QSA,L]

Removing the .php extension with mod_rewrite

RewriteEngine On
RewriteRule something something.php [L]

http://example.com/something will be handled as if it was a request for something.php

To redirect all requests that are not a physical file to the same name but with .php:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]

mod_rewrite rule to remove .php extension and rewrite query string

Try this in your htaccess (which should be in your myproject directory):

Options +FollowSymLinks -MultiViews

RewriteEngine on
RewriteBase /myproject/

RewriteCond %{ENV:REDIRECT_END} !^1$
RewriteRule ^([A-Za-z-_]+)\.php$ $1/%{QUERY_STRING} [R=301,L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule . - [L]

RewriteRule ^([A-Za-z-_]+)/([A-Za-z-_]+)/?$ $1.php?page=$2 [E=END:1,L]
RewriteRule ^([A-Za-z-_]+)/?$ $1.php [E=END:1,L]
RewriteRule ^$ index.php [E=END:1,L]

Every urls, inside your myproject directory, matching that rule (for example something/other-thing) will be rewritten to something.php?page=other-thing

Mod_rewrite to remove PHP extension and redirect

I suggest this redirect rule that removed .php extension and index.php from URLs:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /+(.*?/)?(?:index)?(.*?)\.php[/\s?] [NC]
RewriteRule ^ %1%2/ [R=302,L,NE]

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

mod_rewrite: Hiding PHP extension and showing querystrings as directories

HTACCESS would need to look something like this:

RewriteEngine On
RewriteRule ^([A-z0-9]+)/$ $1.php
RewriteRule ^([A-z0-9]+)/([1-0]+)/$ $1.php?page=$2

For the custom 404 Error you would use:

ErrorDocument 404 /404/

However you will need to make sure that you apply the first rewrite rule to the 404 page.
You will need a 404.php file.

Using mod_rewrite to hide .php from the end of URLs

RewriteEngine On

RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php

Only %{THE_REQUEST} is not rewritten in the internal redirection that happens in the second rule (%{REQUEST_URI}, on the other hand, is).

hide extension .php in url mod_rewrite

The htacces file should look like this:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

You redirect any php file in the url with the same name withouth the "php".

Then in your php files, you can check to see if the url contains the extension (http://blabla.com/bla.php) and redirect the page to the same one withouth the extension.

So, at the beginning of each php file you should call this function :

function redirectIfNeeded(){
$url = $_SERVER["REQUEST_URI"];
if(preg_match("/\.php/$", $url))
header("Location: ".preg_replace("/\.php/",$url));
}

Remove .php extension with .htaccess

Gumbo's answer in the Stack Overflow question How to hide the .html extension with Apache mod_rewrite should work fine.

Re 1) Change the .html to .php

Re a.) Yup, that's possible, just add #tab to the URL.

Re b.) That's possible using QSA (Query String Append), see below.

This should also work in a sub-directory path:

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]


Related Topics



Leave a reply



Submit