15

Ubuntu 14.04LTS 32bit

LAMP

I know it's an old question but..

I need it to remove .php anywhere it finds it from the visible url. It needs to work with /showthread.php?id=XX ---> /showthread?id=XX

I can't even get it to work with /page.php --> /page. I've tried these:

Remove .php extension with .htaccess

How to hide the .html extension with Apache mod_rewrite

Remove .php from urls with htaccess

How to stop .htaccess loop

It just does nothing at all. While other .htaccess code works fine..

While

<?php 
phpinfo();

Lists mod_rewrite in Loaded Modules

And

<?php
 if(!function_exists('apache_get_modules') ){ phpinfo(); exit; }
 $res = 'Module Unavailable';
 if(in_array('mod_rewrite',apache_get_modules())) 
 $res = 'Module Available';
?>
<html>
<head>
<body>
<p><?php echo apache_get_version(),"</p><p>mod_rewrite $res"; ?></p>
</body>
</html>

Returns Module Available

Tried many more things

# Apache Rewrite Rules
 <IfModule mod_rewrite.c>
  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
 </IfModule>

#

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

#

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

Not even this has any effect whatsoever:

RewriteRule ^page$ page.php [L]

sudo service apache2 restart does not change anything.

Server reboot changes nothing.

I tried clearing other code inside, did not make any change.

I cleared my browser cache 100 times

I'm starting to think that it just hates me. What could possible be causing this??

Community
  • 1
  • 1
Myst
  • 553
  • 1
  • 8
  • 19
  • It might be what you're trying to match. Are you trying to use this in the browser `showthread?id=XX` RewriteRule can not match query strings. So that's probably why it's not working. What is your main goal? – Panama Jack Apr 09 '15 at 15:41
  • Did you see this question http://stackoverflow.com/questions/21617506/remove-html-and-php-extensions-with-htaccess – Amit Verma Apr 09 '15 at 15:53
  • That answer also has no affect for me. I am just checking example.com/page (returns 404) and example.com/page.php (displays .php but works). I'm already quite happy if that works.. – Myst Apr 09 '15 at 18:39
  • Can anyone confirm if these actions could affect this in any way: Server reboot, sudo service apache2 restart, other htaccess code, browser cache – Myst Apr 09 '15 at 18:41
  • 1
    I guess I'll be stuch with .php forever :/ – Myst Apr 29 '15 at 10:44
  • What you are trying to do is hell, You would be better off showing your php application how to deal with all requests that are not php through a single entry point such as index.php and then use FallBackResource on it. – Daniel Ferradal Feb 21 '17 at 08:01
  • @Myst Try the snippet in my answer it will work for you as you want. –  Feb 21 '17 at 20:29
  • @ezra-s How would one go about that? – ckhatton Feb 23 '17 at 14:35
  • @Crimbo most people generate a controller for their applications uri, such as index.php, which will deal with all types of uri specific for their app, and all they have to do in they apache is point FallBackResource to it like `FallBackResource /index.php`, this directive points all uri which go to non existant files to a specific file. – Daniel Ferradal Feb 24 '17 at 07:53
  • @ezra-s Arr, I think I've seen that happening for a website URL. Example: `http://example.com/index.php/some-page` – ckhatton Feb 24 '17 at 11:41
  • @anubhava Good point, but the `.htaccess` is working – ckhatton Feb 24 '17 at 11:45
  • @anubhava Yeah, I did – ckhatton Feb 24 '17 at 11:50
  • @Crimbo not what I mean, the real uri get passed to the index.php through PATH_INFO, when using fallbackresource I mean. – Daniel Ferradal Feb 24 '17 at 12:00
  • 1
    @anubhava It is located in the sub-folder where the php files are. Anyway, I have now found Rao Asif Raza's solution works. Thank you – ckhatton Feb 24 '17 at 13:29

11 Answers11

22

Hope helped.

It's worked for me.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For LocalHost !.php
RewriteCond %{HTTP_HOST} !=localhost
RewriteCond %{HTTP_HOST} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteCond %{REMOTE_ADDR} !=::1

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

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Asif Raza
  • 3,435
  • 2
  • 27
  • 43
8

Check this link.

This is the answer using .htaccess:

RewriteEngine On

# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

Tested it on Windows with WAMP and working.

Community
  • 1
  • 1
Condorcho
  • 503
  • 4
  • 12
2

try this to remove .php extensions completly from your file and to avoid infinite loop:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

This code will work in Root/.htaccess, Be sure to change the RewriteBase if you want to place this to a htaccess file in sub directory.

Siddhesh
  • 1,095
  • 1
  • 11
  • 23
  • Even after writing the correct RewriteBase, it did not work - I still get a '404 Not Found' error when accessing the page without the '.php' extension – ckhatton Feb 23 '17 at 14:32
  • Sorry for the delay. Just checked my live site htaccess file, and following code is working for it. RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] – Siddhesh Feb 24 '17 at 08:54
  • I'm afraid that does not work. Also, it is not the 'index.php' that I am trying to rewrite – ckhatton Feb 24 '17 at 11:32
  • I have now found Rao Asif Raza's solution works. You solution was just missing `Options +FollowSymLinks -MultiViews`. Thank you for the help! – ckhatton Feb 24 '17 at 13:21
0

Maybe this could be a duplicate, but take a look at this:

How to remove .html from URL

In the solution, just change html to php

corn on the cob
  • 2,093
  • 3
  • 18
  • 29
Jordan Cortes
  • 271
  • 1
  • 2
  • 16
0

You may be on the right track. However, it sounds like your .htaccess file is not being executed. Just because a module is activated, does not mean it is available for you in your particular situation.

Here are some steps to solve your issue:

  • First of all, check the spelling very carefully. Verify that it is spelled correctly (including the . at the beginning)
  • Check the file permissions. Some servers are going to require executable permissions. So chmod the file to 755.
  • If you still do not have it working, go into your apache configuration (probably at /etc/apache2/apache2.conf on Ubuntu) and find every instance of AllowOverride. It might be set to 'none'. Change this to AllowOverride all instead.
  • Then go into sites-enabled, find your site configuration, and change the AllowOverride fields there are well.
  • Restart your Apache server and congratulate yourself with a big cup of coffee.

One of these should fix it. I would recommend trying between each step so that you can pinpoint where the error occurred. After determining the cause, you may want to go back and restrict some of those AllowOverrides, depending on your needs.

Best of luck!

Terry
  • 989
  • 8
  • 29
0

You can try the following.

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php$2 [L]
  • I'm afraid that does not work. I still get a '404 Not Found' error. I seem to remember trying that before as well – ckhatton Feb 23 '17 at 14:10
  • @Crimbo this snippet is tested in xampp locally and works perfectly so i am guessing you must set `Rewritebase` to the proper folder if needed. –  Feb 23 '17 at 19:47
  • I set the correct `RewriteBase` and it still didn't work. I am beginning to wonder if it is the Apache2 server itself not configured correctly? – ckhatton Feb 24 '17 at 11:35
  • I have now found Rao Asif Raza's solution works. Thank you for the help! – ckhatton Feb 24 '17 at 12:45
0

It works for me:

    #On rewrite
    RewriteEngine On

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d

    # rewrite domain.com/username.php to username
    RewriteRule ^([A-Za-z0-9_-]+)/?$ $1.php [NC,L]
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ $1/$2.php [NC,L]
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ $1/$2/$3.php [NC,L]

And more: Look at my rewrite file for social portal like fb:

# allow only from ip range /8 /16 /24 /31
    #Order deny,allow
    #Deny from all
    #Allow from 89.230.0.0/16

    # On rewrite
    RewriteEngine On

    # [NC]- case-insensitive 
    # [L] - Last Rule , stop the rewriting process here 
    # [OR] = Or - If it matches this condition or the next 
    # [QSA] -   Append query string to rewriting url
    # [R] - redirect [R=301] move permanently, 302 - temporarily, 403 - Forbidden, 404 - Not Found,  410 - Gone

    # fix folder redirect images and css js/images/css/media
    RewriteRule ^.+?/((img|css|js|media|upload|posts)/.+)$ /$1 [L,R=301,NC]

    # Allow any files or directories that exist to be displayed directly
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    # does not work on IIS windows server url rewrite when importing
    # RewriteCond %[REQUEST_FILENAME] !-l

    # rewrite example.xx/index.php na example.xx/
    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index\.(php|html?)$ / [R=301,NC,L]


    #portfolio rewrite folder rewrite
    # RewriteRule ^([portfolio]+)/?$ /portfolio/index.php?id=$1 [NC,L]

    # pretty url example.xx/id/post/number 
    # rewrite domain.com/username like twitter or facebook users
    RewriteRule ^([A-za-z0-9_-]+)/?$ index.php?username=$1 [NC,L]
    # domain.com/post/name
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ profil.php?id=$1&menu=$2 [NC,L]
    # domain.com/cat/post/id
    RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([0-9]+)/?$ profil.php?id=$1&menu=$2&page=$3 [NC,L]

    #RewriteRule ^([A-Za-z0-9_-]+)/(ustawienia)/?$ ustawienia.php?id=$1&menu=$2 [NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(wpisy)/?$ profil0.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(fani)/?$ profil1.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(ogladasz)/?$ profil2.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(zdjecia)/?$ profil3.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(video)/?$ profil4.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(ulubione)/?$ profil5.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/(wiadomosci)/?$ profil6.php?id=$1&menu=$2 [QSA,NC,L]
    #RewriteRule ^([A-za-z0-9_-]+)/?$ profil.php?id=$1 [NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/?$ profil.php?id=$1&menu=$2 [NC,L]
    #RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ index.php?id=$1&dir=$2&post=$3 [NC,L]

    # Redirect all subdomains
    # RewriteCond %{HTTP_HOST} ^(.*)\.breakermind\.com
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # Redirect all subdomains
    # RewriteCond %{HTTP_HOST} ^www.ns2.breakermind.com [NC]
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # RewriteCond %{HTTP_HOST} ^ns2.breakermind.com [NC]
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # Redirect from www. to non-www (unhash if need)
    # RewriteCond %{HTTP_HOST} ^www.breakermind.com [NC]
    # RewriteRule ^(.*)$ http://breakermind.com/$1 [R=301,QSA,NC,L]

    # Redirect from http:// to https:// (from no-ssl to ssl)
    #RewriteCond %{SERVER_PORT} 80
    #RewriteRule ^(.*)$ https://breakermind.com/$1 [R=301,QSA,NC,L]


    ErrorDocument 400 /er404.html
    ErrorDocument 401 /er404.html
    ErrorDocument 402 /er404.html
    ErrorDocument 403 /er403.html
    ErrorDocument 404 /er404.html
    ErrorDocument 500 /er404.html
    ErrorDocument 502 /er404.html
    ErrorDocument 504 /er404.html

    #RewriteEngin On
    #RewriteCond %[REQUEST_FILENAME] !-d
    #RewriteCond %[REQUEST_FILENAME] !-f
    #RewriteCond %[REQUEST_FILENAME] !-l
    #RewriteRule ^(.+)$ index.php?c=$1 [QSA,L]
    #
    #        \w = [A-Za-z0-9_]  \d = [0-9]

And here from my blog system:

        Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{THE_REQUEST} ^.*/index\.php 
    RewriteRule ^(.*)index\.(php|html?)$ / [R=301,NC,L]

    # category
    RewriteRule   ^category/?$ index.php?id=0&page=0  [NC,L]
    RewriteRule   ^category/([A-Za-z0-9]+)/?$ index.php?id=$1&page=0  [NC,L]
    RewriteRule   ^category/([A-Za-z0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2  [NC,L]
    RewriteRule   ^category/([A-Za-z0-9]+)/([0-9]+)/([A-Za-z0-9]+)/?$ index.php?id=$1&page=$2&title=$3  [NC,L]

    # autor
    RewriteRule   ^autor/?$ index.php?id=0&page=0  [NC,L]
    RewriteRule   ^autor/([A-Za-z0-9]+)/?$ index.php?id=$1&page=0  [NC,L]
    RewriteRule   ^autor/([A-Za-z0-9]+)/([0-9]+)/?$ index.php?id=$1&page=$2  [NC,L]

    # article, gallery, tags
    RewriteRule   ^article/([A-Za-z0-9]+)/?$ article.php?id=$1    [NC,L]
    RewriteRule   ^gallery/([A-Za-z0-9]+)/?$ gallery.php?id=$1    [NC,L]
    RewriteRule   ^tags/([A-Za-z0-9]+)/?$ tags.php?id=$1    [NC,L]
    RewriteRule   ^archive/([0-9]+)/([0-9]+)/?$ archive.php?year=$1&month=$2    [NC,L]

    # custom page rewrite
    RewriteRule   ^page/([A-Za-z0-9]+)/?$ page.php?id=$1    [NC,L]

    # fix folder redirect images and css js/images/css/media
    RewriteRule ^.+?/((admin|img|css|js|media|upload|posts)/.+)$ /blog/$1 [L,R=301,NC]

    # user or category user.php
    # RewriteRule   ^([A-Za-z0-9]+)/?$ index.php?user=$1  [NC,L]

    # example
    # RewriteRule ^([folder])/([category])/([A-Za-z0-9]+)/?$ index.php?id=$3&cat=$2 [NC,L]
  • Could you please provide some explanation? – xinaiz Feb 22 '17 at 21:07
  • Like Black Moses states, could you provide some extra explanation as what to use? I tried the first section and I'm afraid it didn't work. I still get a '404 Not Found' error when accessing the page with the '.php' extension – ckhatton Feb 23 '17 at 14:16
  • # **without** the '.php' extension – ckhatton Feb 23 '17 at 14:32
  • RewriteRule ^(.*)$ $1.php Try this my example works for folders –  Feb 23 '17 at 22:11
  • I'm not using sub-folders in this situation - otherwise I could just use index.php and not have an issue – ckhatton Feb 24 '17 at 11:55
  • RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.+?)/?$ $1.php$2 [L] –  Feb 24 '17 at 11:59
  • Look at Peter Darmis example below –  Feb 24 '17 at 11:59
  • Yeah, I tried that. I starting to think something is wrong with my Apache2 setup – ckhatton Feb 24 '17 at 12:01
  • On debian server i remeber you need start mod rewrite first: a2enmod rewrite –  Feb 24 '17 at 12:38
  • LAMP x> apt-get install apache2, php5, php5-gd, php5-mysql mysql-server –  Feb 24 '17 at 12:41
  • It tells me "Module rewrite already enabled". Plus I have now found Rao Asif Raza's solution works. Thank you for the help! – ckhatton Feb 24 '17 at 12:42
0

Assuming that .htaccess is being processed, then this super simple .htaccess should work for you:

RewriteEngine on
RewriteRule (.*) $1.php [END]

If it doesn't work, there is something wrong with your Apache configuration. You need to look into that first.

If it works, add the following line before the RewriteRule to allow serving other files:

RewriteCond %{REQUEST_FILENAME} !-f

The END flag is available since Apache 2.3.9.

Rei
  • 6,263
  • 14
  • 28
  • Well it seems the .htaccess is being processed, as it adds '.php' to the URL when trying to access the index, but not for any other routes. So, like you say, maybe the Apache configuration is wrong. Would you know what Apache configuration to set? – ckhatton Feb 23 '17 at 14:21
0
RewriteEngine on //replacement launch
RewriteCond %{REQUEST_FILENAME} !-d //If the requested object is not a folder
RewriteCond %{REQUEST_FILENAME}\.php -f //If the requested object to append the extension php - file
RewriteRule ^(.*)$ $1.php //make the change from concatenating .php
Endios
  • 51
  • 2
  • I'm afraid that doesn't work. Also be careful to use `#` instead of `//` for comments, and the comments should not share the same line as it causes a 500 error – ckhatton Feb 24 '17 at 12:09
  • 1
    I have now found Rao Asif Raza's solution works. Thank you anyway – ckhatton Feb 24 '17 at 13:27
0

Enabling mod_negotiation in my Apache config did the trick for me:

Content negotiation, or more accurately content selection, is the selection of the document that best matches the clients capabilities, from one of several available documents.

HWD
  • 1,547
  • 7
  • 36
  • 72
0

It's working, You can try this code.

1. First create .htaccess file in your main directory and paste the same code after that clear your history and its working.

RewriteEngine on

RewriteCond $1 !^(index\.php|resources|robots\.txt)

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L,QSA] 
habib
  • 2,366
  • 5
  • 25
  • 41