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] 
2

3 Answers 3

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).

Sign up to request clarification or add additional context in comments.

30 Comments

Please take care that you are not looking at cached results too!
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.
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
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?
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
|
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!

1 Comment

Please read the question again. This is something the OP already implemented himself. It is another issue that is actually asked here.
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]` 

2 Comments

mukesh where i put that code cos u have seen my code
At the top also make sure you have RewriteEngine On on the top of .htaccess code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.