1

I know this question has been asked like a 1000 times and I have probably tried like a 1000 suggestions as well, still no go.

I would like to remove the php file extension and substitute it with a slash (the slash is not so important but to remove the extension is).

My project is located here: localhost/~fn/MyProject/

It contains two folders, public and includes. So all the public files are in the public folder: localhost/~fn/MyProject/public/index.php

I have tried so many suggestions already but most of them simply don't work. I am getting either a Internal Server Error, Forbidden or 404. I am putting the .htaccess to the public folder. Mod rewrite is on. No success with anything on stackoverflow and neither external resources ( e.g. http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ ). For example using the rewrite rules from the metioned webpage shows me 403 Forbidden to even access the index. Any hints of what I may be doing wrong? I am really stuck. Thanks a lot!

4
  • 1
    Please provide your entire .htaccess and give us the URL you will use to access localhost/~fn/MyProject/public/index.php. Is it literally that, or something else? Commented Feb 15, 2014 at 20:55
  • It is literally that. I would like to have it working exactly for that very same url. Commented Feb 15, 2014 at 20:57
  • If you're getting 500 Internal Server Errors have you set the AllowOverride property for your web root? httpd.apache.org/docs/2.2/mod/core.html#allowoverride Commented Feb 15, 2014 at 20:58
  • @AndrewMackrodt It is set to 'all'. Commented Feb 15, 2014 at 21:12

5 Answers 5

2

If your htaccess is in public project folder, try with this code

Options +FollowSymLinks -MultiViews
RewriteEngine on

RewriteCond %{THE_REQUEST} ^.*public/(.+)\.php
RewriteRule ^(.*)$ /~fn/MyProject/public/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /~fn/MyProject/public/$1.php [L]
Sign up to request clarification or add additional context in comments.

8 Comments

No luck with this one either, gives me 403 as well.
We're progressing. I edited my code. Try with this last one. I tested it and it worked like a charm here
Loads this localhost/~fn/MyProject/public alright -> index.php. However specifying any file rewrites to this: localhost/Users/Fn/Sites/MyProject/public/i
I reproduced your example and it worked with the code i gave you. Try to clear your browser cache and try again with another file name.
Actually, not all. For example public/kontakt.php rewrites to: localhost/~fn/MyProject/public/kontakt but shows me: The requested URL /Users/Fn/Sites/MyProject/public/kontakt.php was not found on this server. Is it something with my settings I am forgetting? EDIT: Actually, you are right, clearing the cache helped! Still getting 404 tho, let me try the last edit.
|
1

To remove the .php extension from a PHP file for example yoursite.com/demo.php to yoursite.com/demo, you can add the following code to the .htaccess file:

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

Comments

0

If the directory has an Index.php in it, the url will not show the file name when you browse to that folder. example.com/index.php would just show as example.com. You could use PHP includes to pull in each page to the index page in order to hide the file names but this isn't the best practice in general.

Comments

0

Put this in a .htaccess file in localhost/~fn/MyProject/ (so the file will be localhost/~fn/MyProject/.htaccess):

RewriteRule ^(.+?)\.php$ $1/ [QSA,L]

This captures anything ending in .php, strips off the .php, adds a /, and appends any query string as needed.

Comments

0

If you want only remove extension ".php" from index.php you should use mode_rewrite

 RewriteRule ^index.php$ index // or
 RewriteRule ^index.php$ /

But best way is implements mechanism to rewrite all urls and manage in your front script (index.php);

If you want use mode_Rewrite you shuold check if your mod_Rewrite is enabled on your Apache server. Go to apache.conf and check if line

 LoadModule rewrite_module modules/mod_rewrite.so

is uncomented, dont forgot restart apache!

than you can On rewrite rule type in your apache.conf

 <IfModule mod_rewrite>
     RewriteEngine On
 </IfModule>

or include something like in .htacces

  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME}  !-l
  RewriteCond %{REQUEST_FILENAME}  !-d
  RewriteCond %{REQUEST_FILENAME}  !-f
  RewriteRule .* index.php?%{QUERY_STRING} [L]

Now all signs from urls will going to your once index.php. Here you can do with this everything what do you need to do for example u can call some controller.

If you get 403 Forbiden dont forgot check chmod of the file.

Comments

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.