0

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:

https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess

https://stackoverflow.com/questions/1992183/how-to-hide-the-html-extension-with-apache-mod-rewrite

https://stackoverflow.com/questions/15917258/remove-php-from-urls-with-htaccess

https://stackoverflow.com/questions/13832468/how-to-stop-htaccess-loop/13832827#13832827

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??

3
  • Do you mean you want /showthread.php?id=XX to auto redirect to /showthread?id=XX? or do you just want /showthread?id=XX to open as if showthread.php?id=XX was called?
    – Dan
    Apr 21, 2015 at 17:12
  • The last one. Do you have any idea as to why my listed solutions don't have effect?
    – Myst
    Apr 22, 2015 at 10:29
  • Well actually: /showthread?id=XX doesn't exist. I want it to redirect to /showthread.php?id=XX while both with and without .php display without .php in the url bar
    – Myst
    Apr 22, 2015 at 10:32

3 Answers 3

1

Make sure your specific site is enabled to allow that:

$sudo vi /etc/apache2/sites-available/000-default.conf

<Directory "/var/www/html">
     AllowOverride All 
</Directory>

$sudo service apache2 restart
0

Please use this as your .htaccess file:

<IfModule mod_rewrite.c>   
Options +FollowSymLinks -MultiViews    
# Turn mod_rewrite on    
RewriteEngine On    
RewriteBase /    
RewriteCond %{REQUEST_FILENAME} !-f    
RewriteCond %{REQUEST_FILENAME} !-d    
RewriteRule ^(.*)$ index.php?$1 [L,QSA]    
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]    
RewriteRule ^ %1 [R=301,L]    
</IfModule>

In case you want any file name else, you can replace index.php with your file name and extension.

If your directory still shows the index.php file, please enable module rewrite in your host.

3
  • I'm not sure you understand my question. I need it to remove '.php' from all files. e.g. example.com/forums.php -> example.com/forums Your code does nothing, if I replace your 2 index.php s with forums.php it redirects it to the index. Can you please clarify "enable module rewrite in your host." What does your code aim to do?
    – Myst
    Apr 20, 2015 at 16:46
  • I believe that your can create an autoloader like an index.php file and you require all user requests into it like it can include all the code in forums.php upon user request, or you require file.php into the autoloader index.php upon other user request, it can be working as a router otherwords. Then you can place the .htaccess into the same directory where index.php router / autoloader file is placed so it can remove index.php from your URL completely and all the routes will be upon user requests redirected to that index.php file that will be including forums.php, and other php files directly Apr 20, 2015 at 20:13
  • That's right but I don't have that. Do you have any idea as to why my listed solutions don't have effect?
    – Myst
    Apr 21, 2015 at 13:36
0
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

check out this How to Remove php, html Extension from URL using htaccess

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .