1

I used this code

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

This works for some files the e.g. example.com/contact, but doesn't work when I have a .php file that is also a directory. For example, in the root folder:

science.php 
science - folder

The articles are in categories e.g. http://example.com/science/themost-blabla.php - this works, the .php extionsion doesn't appear in the URL.

So I want to know if is any possible to hide the .php extension to science.php because when I type example.com/science ... it redirects me to the content of the science folder....

Index of /science directory:

  • afla-care-a-fost-primul-meci-televizat-de-fotbal-din-lume-1937-arsenal.php
  • cazinoul-din-constanta.php cele-7-minuni-ale-lumii.php
  • descoperire-colosala-a-epavei-navei-spaniole-san-jose-ce-avea-la-bord-o-avere-impresionanta.php
  • imagini/ mitologia-greaca.php poenaru.php
  • top-10-cele-mai-importante-inventii-romanesti-din-istorie.php
  • top-5-enigme-ale-lumii.php turnul-eiffel.php

So, can I do something to hide the extension to this page? Or do I need to change the name of the file - to not be the some as the folder?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Stefan J.
  • 93
  • 9
  • There's this: https://stackoverflow.com/questions/2358178/running-php-without-extension-without-using-mod-rewrite – ŽaMan Jan 02 '16 at 18:44
  • You cannot rewrite the url to a file if there is a folder named the same as a file. The ressource needs to be uniquely identifiable and thats not the case anymore if you remove the extension. – Charlotte Dunois Jan 02 '16 at 20:11
  • What you can do is to move the file into the science folder and name it index.php, this solution wouldn't require url rewriting too. – Charlotte Dunois Jan 02 '16 at 20:14
  • @CharlotteDunois "You cannot rewrite the url to a file if there is a folder named the same as a file." - Well, you can. The "folder" includes a trailing slash, whereas the file does not. However, you need to set `DirectorySlash Off` in order to avoid conflict. – MrWhite Jan 12 '16 at 01:23
  • @w3dk Doing that will downgrade your searchenginerank since both pages with and without trailing slashes should be the same content (better to make a 301 redirecting from one to another to avoid getting double results). It's bad practice to deliver two different results with and without trailing slashes and will only bring up confuses. Also it shows you are not thinking much as a developer if you thinking this is the solution to your "problem". – Charlotte Dunois Jan 12 '16 at 03:53

2 Answers2

0

Try this

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
  • Hey, I gone through your answer then I understood properly. It's my mistake, thanks for the correction.... – Gouri Shankar Jan 03 '16 at 02:51
  • Providing the OP doesn't need to access the bare directory (eg. `science/`) and is always accessing files, then if you include the `DirectorySlash Off` directive (to prevent mod_dir from appending a slash) then this solution does go someway to solving the problem. – MrWhite Jan 12 '16 at 00:25
0

One of the "problems" is that mod_dir will try to "fix" the URL when accessing a directory by appending a slash to the end of the URL. However, this can be disabled.

# Prevent mod_dir from automatically appending slashes to directories
DirectorySlash Off

# Disable directory listings
# In cases where there is a directory with no similar .php file and no DirectoryIndex
Options -Indexes

# If a PHP file exists for the requested URL then rewrite to this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule (.*) $1.php [L]

If required, bare directories (or rather, the DirectoryIndex) can be accessed by explicitly appending a slash to the URL. eg. example.com/science/. However, this is presumably unnecessary (and probably best avoided to avoid user confusion) since I assume example.com/science (no trailing slash, ie. science.php) returns your "science" category content. Without a DirectoryIndex document, example.com/science/ will simply return a 403 Forbidden. Alternatively you could explicitly remove trailing slashes from such URLs with an external redirect.

MrWhite
  • 43,179
  • 8
  • 60
  • 84