Having problems with RewriteRule and .htaccess - php

I'm having a problem with my .htaccess. This is the code:
RewriteEngine On
Options -MultiViews
Options +FollowSymlinks
#Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#Profile
RewriteRule ^profile/([a-zA-Z0-9_-]+)-([0-9]+)/([a-zA-Z0-9_-]+)$ profile.php?one=$1&two=$2&three=$3
I want to remove the .php extension, and create a seo-friendly URL with /profile.php. The problem comes when I try to access to http://website.com/profile/1/2/3, I get 500 Error. But if I remove the code which removes php extension, then, it works well, but of course, the .php extension is showed.
So... what's wrong in this code? I checked over and over and I really don't find any bugs... thanks.

I think problem is not using L flag (marking Last rule).
Replace your code with this:
RewriteEngine On
Options +FollowSymlinks -MultiViews
#Profile
RewriteRule ^profile/([\w-]+)/([\w-]+)/([\w-]+)/?$ profile.php?one=$1&two=$2&three=$3 [L,QSA]
#Remove .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

How to remove .php extension only for file after the first slash?

I am trying to remove the .php extension only from the file after the first slash.
Right now I have:
example.com/file.php/123123
What I want is:
example.com/file/123123
I have tried adding this code to the .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(e)/?$ /$1.php [L,NC]
It successfully removes the .php extension but only for the last part of the URL.
Meaning it reads:
example.com/file/123123
as:
example.com/file/123123.php
and then makes it unable to load because there is no 123123.php.
How can I make it so it only tries to remove the .php from the after the first slash?
Thanks
If you have mod_rewrite module installed and enabled, you can paste the following code into your apache2.conf file:
<Directory /path/to/site/folder>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule (.*) $1.html [L]
</IfModule>
</Directory>
This module can be installed by default, so you can just try with this code. It may work.

Issue with .htaccess for friendly urls

This is my currently my .htaccess file
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC]
RewriteRule ^(.*)/edit/(\d+)?$ $1/edit.php?tag=$2 [NC]
As you can see it's set to hide the .php extension, but this then seems to break the edit rule. If I comment out
RewriteRule ^(.*)$ $1.php [NC]
The edit rule works fine, but I need both and cannot seem to get it to work, anyone see what the problem is and how to sort it.
[Edit]
I have a link like this and when all rules are active this is what doesn't work.
http://www.domainname.com/researcher/lists/edit/
and I get an 500 Internal Server Error.
Try rules as in your site root .htaccess:
Options -MultiViews
RewriteEngine on
RewriteRule ^(.+)/edit(?:/(\d+))?/?$ $1/edit.php?tag=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [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 issue under WAMP server

I'm trying to change some of the url names using htaccess.
ex;
mg_com_tr/index.php to mg_com_tr/home
I tried many different code samples inside my htaccess file, but nothing seems to work.
I never worked with the htaccess file before,so in order to test it, I found some example codes just for removing the ".php" extension.
When I try this code, I'm not getting any errors, but its not removing the .php extension either.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The only working code I found is this one;
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
This removes the .php extension, however whatever I try I couldn't manage to change
http://localhost/mg_com_tr/index.php
into
http://localhost/mg_com_tr/home
I have a feeling that it has something to do with the path
Both the index.php and htaccess file is located at
D:\wamp\www\mg_com_tr\
try
RewriteEngine on
RewriteRule ^home index.php [NC,L]
or
RewriteRule ^home /index.php [NC,L]

Want to display .php file as without extension?

my url is :http://www.example.com/home.php i want to show it when enduser navigates my site as http://www.example.com/home. I know this can be done through .htaccess file.
Following is the htacces file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php
Nothing is happening with the above code in .htaccess file.I want to show entire site pages without .php extension.Can anyone suggest me how to do this
Try this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>

Categories