1

This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php

Just to be clear - localhost/myfolder/ is a root for my project.

RewriteEngine On     

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]

RewriteRule     ^team-news/([0-9]+[/])?$    posts.php?p=$1&cat=Team\ News
RewriteRule     ^product-news/([0-9]+[/])?$    posts.php?   p=$1&cat=Product\ News
RewriteRule     ^member-specials/([0-9]+[/])?$    posts.php?p=$1&cat=Member\ Specials
RewriteRule     ^ambassador-blogs/([0-9]+[/])?$    posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule     ^user/([0-9]+[/])?$         profile.php?id=$1
RewriteRule     ^browse-all/([0-9]+[/])?$   searchall.php?p=$1
RewriteRule     ^edit/([0-9]+[/])?$         edit.php?id=$1
RewriteRule     ^articles/([0-9]+[/])?$     post.php?id=$1    
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Danijelb
  • 61
  • 6
  • Not sure what you do wrong, but there are some tools like [this](http://htaccess.madewithlove.be/) that might be able to help you out.. – Naruto Feb 11 '15 at 16:04
  • Can you clarify what the error is and which URL isn't working? – anubhava Feb 11 '15 at 16:04
  • The code that is posted here all works, but now I need to insert a part which removes .php from the files, and whatever I try just won't work. – Danijelb Feb 11 '15 at 16:06

4 Answers4

1

This snippet will allow you to rewrite to remove php extensions:

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

If you want your URL to have a trailing /, you can use this snippet:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Source

Blake Frederick
  • 1,510
  • 20
  • 31
0

Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:

UPDATED:

PHP:

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

HTML:

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

those will remove all extensions in your files (both php and html). Note: see if server enables mod rewrite module/extension.

Joe Kdw
  • 2,245
  • 1
  • 21
  • 38
  • This gives me infinite loop, for example the /myfolder/search.php path it goes to /myfolder/search/.php/.php/.php/ to the infinity – Danijelb Feb 11 '15 at 16:25
  • I updated my answer, see if it helps. don;t forget to check if server (apache/else) enables you to have the action – Joe Kdw Feb 11 '15 at 16:55
  • I checked, it does. The rules you see I posted all work, for example localhost/myfolder/user/6/ is working. Adding trailing slash rule works too. But whatever rule I insert to remove .php it throws an error. Maybe it's interfering with some of my other rules? – Danijelb Feb 11 '15 at 17:12
  • Yes, it's the whole file for now. – Danijelb Feb 11 '15 at 17:18
  • do you have access in the web root, I mean at localhost, not folder. ? – Joe Kdw Feb 11 '15 at 17:26
  • Yes I do, it's XAMPP that I set up to develop an app for a client. Never messed with rewrites before. – Danijelb Feb 11 '15 at 17:27
  • Then, try to remove 'RewriteCond %{REQUEST_FILENAME} !-f' above 'RewriteCond %{REQUEST_URI} !(.*)/$' and try this tips> http://stackoverflow.com/questions/10364951/make-web-root-folder-a-sub-folder-with-htaccess – Joe Kdw Feb 11 '15 at 17:31
0

Just below your 301 rule add this rule:

RewriteEngine On
RewriteBase /myfolder/

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

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • It gives me 404 error, for example for /myfolder/search.php path it goes to /myfolder/search/ but with 404 – Danijelb Feb 11 '15 at 16:23
  • Still nothing, now it adds a /.php/ to my other urls, and throws 404 error for php files. Maybe I am doing something wrong in my other rules that messes everything else up? – Danijelb Feb 11 '15 at 16:34
  • These 2 rules work fine for me. Comment out your trailing slash 301 rule and retest. Where is this .htaccess located? Inside `myfolder` or a level above it? – anubhava Feb 11 '15 at 17:17
  • When I remove slash 301 rule and insert these two I get 404 errors. .htaccess is located inside myfolder - which should act as a root of my project. I am doing it that way because whole project will be located inside a separate folder on a domain. – Danijelb Feb 11 '15 at 17:43
0

You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):

Root .htaccess:

RewriteEngine On     

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]

myfolder .htaccess:

RewriteEngine On
RewriteBase /myfolder/

RewriteRule ^team-news/([0-9]+[/])?$         posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$      posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$   posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$  posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$              profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$        searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$              edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$          post.php?id=$1 [L]

Note how I have also included [L] to stop it from doing anything else once it has found a match.

Mike Rockétt
  • 8,947
  • 4
  • 45
  • 81