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    
3
  • Not sure what you do wrong, but there are some tools like this that might be able to help you out.. Commented Feb 11, 2015 at 16:04
  • Can you clarify what the error is and which URL isn't working? Commented Feb 11, 2015 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. Commented Feb 11, 2015 at 16:06

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

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.

7 Comments

This gives me infinite loop, for example the /myfolder/search.php path it goes to /myfolder/search/.php/.php/.php/ to the infinity
I updated my answer, see if it helps. don;t forget to check if server (apache/else) enables you to have the action
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?
Yes, it's the whole file for now.
do you have access in the web root, I mean at localhost, not folder. ?
|
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.

Comments

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]

4 Comments

It gives me 404 error, for example for /myfolder/search.php path it goes to /myfolder/search/ but with 404
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?
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.