Rewriting /foo to /foo.php doesn't work - php

I want users to be able to go to /foo and have /foo.php displayed. A quick search on Google came up with this:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
For some reason, however, this doesn't work. I keep getting a 404 error, even though I know the .php file exists. I searched and searched for an answer, but it seems to work fine for everyone else.
I commented out the other rules, by the way, so nothing is conflicting with this.
Any ideas?

This is what you need:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Maybe you need to add the directive AllowOverride All in your apache configuration or maybe you need to add RewriteEngine On at the very beginning of your .htaccess file

You need this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /$1.php

I found the answer! Apparently this is a problem with 1and1's server configuration. This article explains the problem: http://tips.webdesign10.com/web-hosting/why-you-should-never-use-1and1-com-hosting
The first comment also happens to be the solution. Here's the rule that works:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]

alternatively you can use apache's MultiViews option:
Options +MultiViews

You get this issue when you use .htaccess in a subfolder. Fix it by using full paths from the web root in your .htaccess file. For instance, in the folder http://www.mysite.com/test/ your .htaccess file would look like this:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /test/$1.php
-not tested, might need tweaking-

Related

htaccess Rewrite directory to php pages

Say I have these pages stored in the root:
page1.php
page2.php
how do I rewrite this:
domain.com/page1/
to:
domain.com/page1.php
P.S.Also need to make sure the rewritten url does not clash with a real directory.
UPDATE:
Solutions below were only working on my remote server, but not localhost Xampp server.
I added this first line to my htaccess and solutions below worked on my local server!
Options +FollowSymLinks +MultiViews
You may use this rule in your site root .htaccess:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You can use the following Rule in /root/.htaccess :
RewriteEngine On
#skip real files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite /file/ to /file.php if /file/ exists as .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [L]
If you just want to redirect a small number of pages, you could do this:
RewriteRule ^page1/?$ /page1.php [NC,L]
RewriteRule ^page2/?$ /page2.php [NC,L]
You might want to consider something like the following, though, where you pass the number as an argument (assuming you are following a naming convention as you described).
^page/([0-9]+)/?$ /pageLoader.php?p=$1 [NC,L]

Can't remove .php from website using htaccess. Dreamhost server

I am trying to remove the ".php" on my website. I have changed the .htaccess file based on the documentation provided in the dreamhost wiki to the following:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
However, this does not work on the site. For example this code does not work:
link
It shows the following error:
Not Found
The requested URL /jimmyvosler.com/public/about.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
This works (but still displays the .php at the end, which is what im trying to fix):
link
I don't understand what is causing this, especially since the error even points to the correct place. Is there a setting I need to check on my admin tool?
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Here is the solution for anyone else that has the same trouble. I added this to my .htaccess file. This solution came from dreamhost support and was linked from this website: http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Try this:
Options All -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]

.htaccess redirect from subdirectory to parent directory file but maintain url path

My apologizes if this has been answered but I haven't found anything that works for me. This is on a cheap godaddy shared server, php based.
I have tried a few RewriteRules but to no avail.
Basically, if you are linked to something like :
www.example.com/items/apple
It would keep that in the url, but instead go to:
www.example.com/items
In the items page, I would be able to get the /apple part and do with it as I will. Either having it passed in through parameter like ?dir=apple or just as the /apple. My problem right now is the redirecting. My .htaccess file looks like this
RewriteEngine On
Options -Multiviews
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
Try adding an additional condition and/or switch the order:
RewriteEngine On
Options -Multiviews
RewriteRule ^item/([^/]+) /item?dir=$1 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Remove .php extension is not working

I found a lot of tutorials and questions regarding to removing the .php extension in the URL. I have tried a lot of examples and until now it is still not working. This drives me crazy. I also want to put trailing slash at the end of the URL but first I would like to achieve this first. Currently I have tried this which is some of my solutions:
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
and also this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Nothing is good. I am working in my laptop using windows 7, and using XAMPP. I simply created my .htaccess file and put it in localhost/myFolder. When I run in the browser, depending on my solution, (i) some times I got page not found, (ii) sometimes server internal error. Also when I run for example localhost/myFolder/index the URL will be redirected to localhost/index without myFolder. I wonder why is this happening and hope somebody can give me links or solutions because I tried so many of them. Thank you in advance.
The following works fine for me:
RewriteEngine On
RewriteBase /myFolder
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])/?$ $1.php [L]
Try this code in /myFolder/.htaccess for hiding .php extensin:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/myFolder/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
A solution for your problem:
Options +MultiViews
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/myFolder/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
I just replaced your - sign in multiviews and it started working on my localhost

Using .htaccess to URL rewrite on windows

I have have turned mod_rewrite on in a windows machine and created .htaccess in the root directory of my website, now after i wrote FAIL in it it gave me an internal server error then i replaced the code with this
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ /$1.php [L,QSA]
I am trying to remove .php and make friendly URLs, when i am tring to load index without using .php it is giving me a 404 Not found error i have restarted the wamp server several times and the same problem. Can somebody help me here.
Thanks in advance
I have changed the .htaccess to this
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
and it worked just in case somebody faces the same problem
Remove the line RewriteCond %{REQUEST_FILENAME}.php -f. It is unnecessary.

Categories