3

in htacesss file in admin folder, removed php extension on wamp although worked fine. when moved to ubuntu with lamp server. it's giving 404 not found. but htaccess working perfectly for other thing like index routing.Below what i am using on htaccess file.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex index.php
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
Muthukrishna C
  • 147
  • 1
  • 13
  • What is the URL that generates the 404? Change the rewrite into a 302 redirect (you'll need to explicitly set the URL-path when you do this) to see what it's actually redirecting to. – MrWhite Jan 13 '16 at 15:39
  • can you explain breifly, the url i am getting is http://localhost/work/projects/sellyourgold/admin/users – Muthukrishna C Jan 13 '16 at 15:41
  • Try (temporarily) changing it to `RewriteRule ^([^.]+)$ /path/to/admin/$1.php [R,L]` (`/path/to/admin/` should be the directory in which the .htaccess is located). – MrWhite Jan 13 '16 at 15:44
  • tried, still not working. is it because of any configuration issue or previously returned rules? – Muthukrishna C Jan 13 '16 at 15:50
  • That isn't intended to fix it - it's to help you debug the problem. I assume this generates a 404 still? Is it redirected? What is it redirected to? – MrWhite Jan 13 '16 at 15:51
  • that's not redirecting, it's giving 404 – Muthukrishna C Jan 13 '16 at 15:54
  • If it's not redirecting then the code isn't even being executed (or the pattern doesn't match, but that is unlikely). So now you have a different problem... why isn't it being executed? What other directives do you have in your .htaccess file? Do you have other .htaccess files in parent directories? – MrWhite Jan 13 '16 at 15:57
  • can you tell me why it's working on wamp, but not in lamp – Muthukrishna C Jan 13 '16 at 15:57
  • yes i am having htaccess on parent directory – Muthukrishna C Jan 13 '16 at 15:58
  • There's not enough information here to say why it would work on one system and not another. There is nothing actually wrong with the small snippet of code you have posted. So you need to look elsewhere. Just try adding `anything` to the start of this .htaccess file - you should get a 500 error. – MrWhite Jan 13 '16 at 16:01
  • yes i tried and getting error, also directory index also working – Muthukrishna C Jan 13 '16 at 16:03
  • 1
    You've not stated what other directives you have in your file and what directives you have in the parent .htaccess file. Is the directory the same between WAMP and LAMP servers? Is mod_rewrite enabled? Try adding `RewriteRule ^ http://www.example.com [R,L]` - do you get redirected? – MrWhite Jan 13 '16 at 16:07
  • redirecting if i try from /admin, if i try /admin/users not redirecting – Muthukrishna C Jan 13 '16 at 16:13
  • So, what is different about those requests? What is `users`? A file? Directory? Do you have any directives in your server config? – MrWhite Jan 13 '16 at 16:40
  • 1
    it's because of i put in sites enabled config file, i removed it it's working fine. – Muthukrishna C Jan 13 '16 at 17:20

1 Answers1

3

Try this rule:-

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

OR

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

OR

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

If this global rules is not working for you then try below code also:-

In this code replace 'work' with your project root directory.

RewriteEngine on
RewriteBase /work/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !\.php$
RewriteRule .* $0.php [L]

In linux you need to ON rewrite module.

open your terminal(ctrl+alt+t) and run this command.

sudo a2enmod rewrite

then restart apache2 by this command:-

sudo service apache2 restart

This tutorial link will help you.

Hope it will work for you :)

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42