6

I recently updated my .htaccess file to hide the .php extension of the urls. It works nice, when I try to reach domain.com/index, it displays the php file.

When I type domain.com/index.php it redirects me to the domain.com/index url and displays the .php file as well.

I would like to redirect all other extensions to the no extension url to display the .php file for security reasons. My problem is, that I can not configure my .htaccess to redirect well known extensions like .html .asp. aspx .shtml etc. to the non-extension url, and get the .php file displayed as the content.

My .htaccess file looks like this:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]


# no extension to the .php file
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]

# this is for the index
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
2

3 Answers 3

1

Try these in your .htaccess file and please check if rewrite engine is on in server and there is mod_rewrite.c module is there in main apache conf file that is httpd.conf.

1. RewriteRule ^about$ about.php [L] OR 2. RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)$ $1.php

Hope these work fine.

OR otherwise this is another option with all description:

Apache Rewrite Rules Options +FollowSymLinks RewriteEngine On RewriteBase /

Add trailing slash to url

RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/|#(.))$ RewriteRule ^(.)$ $1/ [R=301,L]

Remove .php-extension from url

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

End of Apache Rewrite Rules

Things I guess you want to know:

  1. This is for sites that don’t use a front-end-controller where all requests are forced through index.php. If you need that, you must replace the third block with your own.

  2. The example is showed with .php only. If you want to use it with .html for example, you must copy/paste the third block and replace “.php” on line (1,) 3 and 4 with “.html” (unless someone has a neat way of doing it?)

  3. The content of “example.com/something/” could be served from either “example.com/something.php” or “example.com/something/index.php”.

  4. To link to other pages, just href=”/some/thing/” or href=”//example.com/some/thing/”.

  5. With .css and .js you want to href=”/style.css” or href=”/folder1/style.css”. Else if you href=”style.css” from “example.com/folder2/file.php” it’ll look at “example.com/folder2/style.css”.

Note: There is http:// in-front of all all example.com.

0

if there's no other "." in your files except for the extension, did you try replacing ".php" by any string?

# php extension to no extension url
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.\s+\sHTTP/.+
RewriteRule ^(.+)\.php $1 [R=301,L]
0

Try The Below Code and change the extension whatever you want to hide:

1.For html file

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [L]

2.For php file

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .