0

I want to remove the .php filename extension from the URL, I have already written code in the .htaccess file but I am missing something because by default when I open the page it doesn't have the .php extension, but if I manually add the .php extension in the URL then the page also opens, which I want to avoid.

.htaccess file:

RewriteEngine On
RewriteBase /

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

# rewrite category
RewriteEngine On
RewriteRule ^blog-category/(.*)$ blog-category.php?category=$1 [NC,L]

# rewrite blog
RewriteEngine On
RewriteRule ^blog/(.*)$ blog.php?title=$1 [NC,L]

# error pages
RewriteEngine On
ErrorDocument 404 /404.php

# on 301 error redirect to softcrayons.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^softcrayons.com
RewriteRule (.*) http://www.softcrayons.com/$1 [R=301,L]
LoïcR
  • 4,940
  • 1
  • 34
  • 50
arif khan
  • 23
  • 7

3 Answers3

1

You have nothing in that dynamic configuration file that actually prevents scripts being called directly. You have to add another redirection for that:

RewriteEngine On 
RewriteBase /

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

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

This will force an external redirection (so change the URL visible in the browser) and a second request for all requests that use the .php file name extension and where that file actually exists.

Note that you may have to take care to not create an endless rewrite loop.

I also added some additional condition to only internally rewrite to .php if that file actually exists.


If you really want to create an error, a http status 404 for requests to URLs that have the .php file name extension then replace the rewriting rule in the code above like that:

RewriteEngine On 
RewriteBase /

RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?([^.]+)\.php$ - [R=404]

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

Note however that as already said I think that is a stupid thing to do. Why frustrate your users with an error? You know what they actually want and you can fulfill that request. Think positive!


And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

arkascha
  • 41,620
  • 7
  • 58
  • 90
  • Please take care that you are not looking at cached results too! – arkascha May 10 '17 at 06:39
  • I repeat my question which you did not answer: "is not working" means what _exactly_? Do you get any entries in your http servers error log file? Do you get an error in your browser? What does your browsers console say about that request? What happens? A plain "does not work" does not help, sorry. – arkascha May 10 '17 at 06:47
  • see i dont want .php exrtention in url and when i click any page the url working fine its not showing .php extention but in those file when i edit .php in url then its also open that i dont want and browser is not showing any error – arif khan May 10 '17 at 06:50
  • We all understood that, there is no need to repeat it here. But you _still_ did not answer to my question. May I ask _why_ not? – arkascha May 10 '17 at 06:51
  • you just go softcrayons.com and click any page u will get url without .php extention but when u add .php extention in same url then the page also open that i dont want i want when someone add .php in url then its redirect on 404 page – arif khan May 10 '17 at 06:52
  • Note that the above code is not meant to block or prevent reqests to `/foo.php` (that makes little sense) but _redirects_ them to `/foo`. That means the page _is_ indeed opened, but the visible URL in the browser changes. That _probably_ is what you want. – arkascha May 10 '17 at 06:52
  • Why would you want to redirect to a 404 status message? That makes no sense! – arkascha May 10 '17 at 06:53
  • see if someone try to add .php extention in current url then the page should not be open – arif khan May 10 '17 at 06:56
  • You did understand what I said above? That such rule makes no sense? Why would you want to throw an error in your users face when you can serve the request? The important thing is to redirect them so that the `.php` _is removed_ from the URL. – arkascha May 10 '17 at 06:57
  • give me your skype account then i show youmy project – arif khan May 10 '17 at 06:58
  • Ok, I added a variant that throws a 404 error for such requests. But once more: note that this is a stupid thing to do in my eyes. – arkascha May 10 '17 at 07:01
  • see i am working for seo perpuse like if i do seo for http://localhost/core-php-training and someone try to open http://localhost/core-php-training.php then this is not part of my seo so that is another thing which i dont want – arif khan May 10 '17 at 07:04
  • I think you do not really understand what I tell you. Anyway, go ahead with that second option I added. – arkascha May 10 '17 at 07:05
  • give me your skype id please – arif khan May 10 '17 at 07:07
  • Sorry, but we do not exchange personal communication contacts on this page. The code is right in the answer above. I modified it and added a second, alternative code block. – arkascha May 10 '17 at 07:08
  • u didn't understand what i try to say – arif khan May 10 '17 at 07:10
  • You see TWO answers above, use the SECOND one. That will throw an error as you want. – arkascha May 10 '17 at 07:11
  • i am just saying that if some open this localhost/core-php-training thats ok but if someone try to acees like this localhost/core-php-training.php then that should not be open thats it – arif khan May 10 '17 at 07:12
  • https://chat.stackoverflow.com/rooms/143838/remove-php-extension-with-the-help-of-htaccess – arkascha May 10 '17 at 07:17
  • i didn't find chat option where is – arif khan May 10 '17 at 07:20
  • my skype id is 00786arif@gmail.com send me request and save my job please – arif khan May 10 '17 at 07:25
  • In that [chat room](https://chat.stackoverflow.com/rooms/143838/remove-php-extension-with-the-help-of-htaccess) you can enter your message at the bottom to the left. In the yellow area. – arkascha May 10 '17 at 07:31
  • i got your message only there is no option – arif khan May 10 '17 at 08:02
  • i have already write code for 404 and it has work cos if i try to add somthing in url then its redirerct on 404 – arif khan May 10 '17 at 08:07
  • please send me request on my skype id – arif khan May 10 '17 at 08:07
  • And once more: we don't use private communication channels here. I have very bad experiences with that. – arkascha May 10 '17 at 08:10
  • my question is that if someone try to add anything else in my current url then it will be redrect on 404 and that is wotking but if someone try to add .php in my url then the page should not be open – arif khan May 10 '17 at 08:12
  • And it does not but delivers a status 404 too if you use the code above. – arkascha May 10 '17 at 08:18
  • I make one last attempt. I am sorry if communication is not possible, maybe for language reasons. But that is nothing I can help. I have the impression you do not really read what I write, do not really try to understand what I offer. The code you need is in the answer. Two variants. You ask for the second variant. It leads to a 404 if someone requests `....php`. That is what you ask. Not a good solution, but what you ask. – arkascha May 10 '17 at 08:29
  • leave it what i am trying to say u didn't understand – arif khan May 10 '17 at 08:45
0
## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]`
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

HTML:

<a href="home">Index</a>

.htaccess

 RewriteEngine On
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule ^([^\.]+)$ $1.php [NC,L]

Link will redirect you to the home.php file, and your url will be example.com/home hope this will help you.

Greetings!

Geee
  • 2,217
  • 15
  • 30
  • Please read the question again. This is something the OP already implemented himself. It is another issue that is actually asked here. – arkascha May 10 '17 at 06:45