0

I am using following code to remove .php extension from URLs:

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

My question is how to keep it removed just like stack overflow does. If my URL is domain.com/about.php it is redirected to domain.com/about but if someone types .php after about intentionally it remains there. Also, if someone directly visits domain.com/about.php it still remains there. How can I remove any extensions from URL just like stackoverflow does even if user types it intentionally? If this can't be done in .htacess can I do it in JavaScript or PHP?

SanJeet Singh
  • 1,291
  • 2
  • 15
  • 28
  • if you are using codeigniter, it will never give .php as all views is called from controller's side(MVC pattern), so you can give your controller and view name same... – Abbas Nov 17 '15 at 04:04
  • The term you're looking for is, "routing" – ndugger Nov 17 '15 at 04:05
  • Possible duplicate of [Remove .php extension (explicitly written) for friendly URL](http://stackoverflow.com/questions/9821222/remove-php-extension-explicitly-written-for-friendly-url) – diziaq Nov 17 '15 at 04:09

2 Answers2

0

Try :

RewriteEngine on

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

This will redirect

http://example.com/file.php

to

http://example.com/file

and then rewrite :

http://example.com/file

to

http://example.com/file.php
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thanks. However, If it is redirecting `.php` to `not php` and then rewriting it to `.php` then what purpose does it serve? I want to keep the extension removed. – SanJeet Singh Nov 17 '15 at 04:15
  • This remove the .php extension completly, meaning that if you type http://example.com/file.php you will be redirected to http://example.com/file – Amit Verma Nov 17 '15 at 04:19
  • @Strakeen Could you please explain what each line does? This will help me to write my own `.htaccess` next time. If not that could you provide URl of some resourse which I can read to know more about how these `.htaccess` statements work? – SanJeet Singh Nov 17 '15 at 04:26
0

Magic Rewrites for Clean URLs when using Apache Web Server:

This process consists of a few steps. You should feel like a ninja for wanting to do this and put in the extra effort. It's worth it!

Place a .htaccess file in the folder you're working in: (for example, if you're working in directory /company/ you'd put this in /company/.htaccess)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]

So, we've now got a path on your server that will accept anything inside of it, if it doesn't exist. For example, if you point to /company/Joe_Plumber it will redirect to index.php. If you point to /company/style.css and it exists and has correct permissions, it'll give you the file you asked for.

Create an index.php file in that directory. (for example, /company/index.php)

Now, inside your .php file you're going to have to process what the user asked for. Basename will give you only the last folder, so if we requested /company/foo/bar it will only get bar

$q = basename($_SERVER['REQUEST_URI']);
$q = str_replace('_',' ',$q); // Convert any underscores to spaces
echo "Hi, {$q}!";

That's all there is to it. If there is any way I can clarify or help, just ask.

jaggedsoft
  • 3,858
  • 2
  • 33
  • 41