2

I am rewriting my url to get rid of the .php extension (and hopefully the get variables too as all the help I have gotten is great, but still wont work.

Here is my code:

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

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

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([^\.]+?)/?$ $1.php [NC,L] 

Now when I use this code, it takes the .php out, but then the error says:

Not Found

The requested URL /index.php was not found on this server.

Meanwhile, if I take that code out, it works fine, but has the .php extension

user3241507
  • 353
  • 6
  • 18

2 Answers2

5

I use this code in .htaccess to remove .php extension and redirect .php extension pages to non .php extension pages :

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteRule ^(wp-admin)($|/) - [L] # You don't want to mess with WordPress 
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteCond %{REQUEST_FILENAME}\.php -f
 RewriteRule .* $0.php

 # 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]
</IfModule>

When the above code is used in .htaccess, you can access PHP pages without the extension and when a user visits a page with .php extension, he/she will be redirected to the page without the extension.

Example :

http://example.com/page.php is redirected to http://example.com/page

http://example.com/page will be the same as http://example.com/page.php

There wouldn't be any problem when you access PHP files with extension internally :

<?
include("page.php");
?>

will work without any problem.

Subin
  • 3,445
  • 1
  • 34
  • 63
  • This works ! but I am having an issue, it is redirecting me to `localhost/mypage` where it needs to be `localhost/myproject/mypage` – user3241507 Feb 23 '14 at 06:34
  • And how can I take the GET variables out? – user3241507 Feb 23 '14 at 06:37
  • @user3241507 hmmm.. It's working well for me. Maybe there's another code in **.htaccess** that's conflicting with my code. I don't know any way to pass **GET** parameters with the redirect. You should change the .php URL's with `GET` parameters to non .php URLs with GET parameters – Subin Feb 23 '14 at 06:40
  • But how do I get it to access the subfolder and not the main site? – user3241507 Feb 23 '14 at 06:43
  • @user3241507 I updated the code on answer. Try it now. It may work – Subin Feb 23 '14 at 06:46
  • I am not using wordpress, this is a hand coded site. Not to sure how to change that to redirect to the `mysite` sub folder – user3241507 Feb 23 '14 at 06:48
  • @user3241507 Is there any other **.htaccess** files in your root directory or any other folders ? It may conflict with my .htaccess code – Subin Feb 23 '14 at 06:53
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48185/discussion-between-subin-and-user3241507) – Subin Feb 23 '14 at 06:57
  • put this before wordpress RewriteRule for index.php, and it works – Anthony Kal Aug 10 '18 at 02:19
  • As i go with this solution, there something wrong with $_post parameter, all data gone redirected if i post request using /sample-thanks-page.php . `RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]` , but when i remove it and request it like this : /sample-thanks-page ,the $_post data is appear. (i need to remove the redirection there, as i need to still accept those /*.php POST request ? ) – Anthony Kal Aug 10 '18 at 03:53
2

Place this code in /myproject/.htaccess (not in root):

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

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

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/myproject/$1.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

And then test it from a new browser.

anubhava
  • 761,203
  • 64
  • 569
  • 643