Remove php extension from htaccess but keep PATH_INFO functional - php

I make URLs like http://localhost/index.php/add/
The /add/ after the index.php is $_SERVER['PATH_INFO'] and can be used in PHP coding.
I want to remove the .php from this URL using .htaccess and still keep using the $_SERVER['PATH_INFO'], no matter how may segments there are.
For instance,
http://localhost/index.php/add/cars/
http://localhost/index.php/add/cars/japan/
http://localhost/index.php/add/cars/japan/tokyo/
I also want a trailing slash at the end as in the examples above.
I can remove the .php extension using the script below, but I need a bit more than that.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]

You can use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index(/.+)?$ index.php$1 [NC,L]
If you also use another name (and not always index). Change the last line:
RewriteRule ^([^/.]+?)(/.+)?$ $1.php$2 [L]

try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Related

Rewrite .htaccess file but not working

I wish to rewrite this url:
http://salessurface.com/overseas/sub_page_details/MBBSAbroad/MBBS-in-UK/7
into:
http://salessurface.com/overseas/sub_page_details.php?sup_menu=MBBS%20Abroad&menu=MBBS-in-UK&main_menu_id=7
I tried adding the following to the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ overseas/sub_page_details.php?sup_menu=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?sup_menu=$1&menu=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ overseas/index.php?
sup_menu=$1&menu=$2&main_menu_id=$3 [L]
but it is not working.
How do I accomplish that rewrite?
You don't need the conditions, you can do that with just a rule:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/(.*)/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=$1&menu=$2&main_menu_id=$3 [NC,L]
This will take the path after sub_page_details and put the first part in sup_menu, the second part in menu and third part in main_menu_id.
But I noted that in your sample URLs you use MBBS%20Abroad in the real URL but MBBSAbroad in the rewritten URL, if this is not a typo and the real URL has an space on it that doesn't have the rewritten URL then you need to use a less specific rewrite for that option:
RewriteEngine on
RewriteRule ^overseas/sub_page_details/MBBSAbroad/(.*)/(.*) /overseas/sub_page_details.php?sup_menu=MBBS\ Abroad&menu=$1&main_menu_id=$2 [NC,L]
This will hard code the sup_menu part of the URL and take the path after MBBSAbroad and put the first part in menu and second part in main_menu_id.

two RewriteRule's for two different but like urls

My .htaccess is
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
RewriteRule . index.php [L]
so all requests i receive in my index.php and then parse query string, and one of my pages is "api documentation page" with url http://domain.com/api so i require page about api from templates
and another url is http://domain.com/api.php where i need receive $_GET['action'] and another data with $_POST
so it will work like http://domain.com/api.php?action=start, but i need call it like http://domain.com/api/start or http://domain.com/api/start/ and receive $_GET['action'] - "start"
but this string in my .htaccess doesnt work
RewriteRule ^api\/(.*)$ api.php?action=$1 [L,QSA]
You need to keep RewriteCond before every RewriteRule if you want to apply for all URLs or keep them in a separate rule. Try this .htaccess:
Options -MultiViews
RewriteEngine On
RewriteBase /
# ignore all files/directories from all rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^api/(.+)$ api.php?action=$1 [L,NC,QSA]
RewriteRule . index.php [L]
Ah, I've had that problem before, can't remember exactly how it went, but this link seems relevant: https://wiki.apache.org/httpd/RewriteFlags/QSA
The only difference with yours is the slash at the beginning to make the paths absolute and you don't need to escape slashes. E.g.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/([a-z]*)/? api.php?action=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
But none of those differences seem to be the cause of the problem. As pointed out by nubhava, the RewriteCond's only affect the next rule though.
When faced with this kind of situation though, I usually end up using a router, to do the "rewriting" in PHP.
See also Mod-Rewrite or PHP router?
RewriteEngine On
RewriteBase /
RewriteRule ^api/([^/]*)$ /api.php?action=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
work's, http://htaccess.madewithlove.be/ help's me trying different combinations

Using RewriteEngine in PHP to read a file

recently I'm making a php application and I want some changes in my file.
So I use .htaccess and RewriteEngine and when I want to run my application it said 404.
Here is my code that I try and said 404 !!
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^access/(\d+)*$ show.php?address=$1&login=0
for example this my original address:
www.ex.com/show.php?address=5411496bc2b6c&login=0
And I want to change it to:
www.ex.com/access/5411496bc2b6c
Here is my question Am I doing something wrong in .htaccess ?
Try this rule:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^access/([^/]+)/?$ show.php?address=$1&login=0 [L,QSA,NC]
In your rule:
RewriteRule ^access/(\d+)*$ show.php?address=$1&login=0
Should be:
RewriteRule ^access/([a-z0-9]+)$ show.php?address=$1&login=0
(\d+) means digits and as it seems you need digits + lower alpha characters right?
Also SCRIPT_FILENAME will match executing script file name, so it will never be directory and you will also rewrite real existing directories, instead use REQUEST_URI or REQUEST_FILENAME
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
This works for me:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^access/([a-z0-9]+)$ show.php?address=$1&login=0 [L]

URL Rewriting confusion

I got a webpage that has the following URL:
localhost/Minecraft-User-Info/player/?user=matthijs110
Now I want it to localhost/Minecraft-User-Info/player/matthijs110
So without ?user=matthijs110
I tried different ways, but it doesn't seem to work. Mod_Rewrite is enabled.
What can I do to get this to work?
Give this a try.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Minecraft-User-Info/player/(.+)$ /Minecraft-User-Info/player/?user=$1 [L]
Edit:
Use this since it's in a sub directory.
RewriteEngine On
RewriteBase /Minecraft-User-Info/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^player/(.+)$ player/?user=$1 [L]
Updated to show my original answer and included the edit per users comment about where htaccess is located.
Try this instead. Minecraft-User-Info/player/username Redirects to Minecraft-User-Info/player/index.php?user=username
Tested on http://htaccess.madewithlove.be/ instead of setting up my own server for it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^Minecraft-User-Info/player/(.+)$ /Minecraft-User-Info/player/index.php?user=$1 [L]
Try this
RewriteRule ^Minecraft-User-Info/player/([a-zA-Z0-9_-]+)\$ index.php?playerId=$1

htaccess remove .php and keep query string

Here is my .htaccess file right now.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
This works in the fact that it makes my pages accessible when not using the .php extension.
Old = domain.com/test.php
New = domain.com/test
The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?
domain.com/test?id=1
Matching the entire query string and appending it to your new URL using a back-reference should work.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If you are passing id then you have to use this code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1
now you can access
domain.com/test?id=1
from
domain.com/test/1

Categories