using htaccess to remove .php from URL - php

MY URL: http://localhost/test.php
I am using:
.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
PHP:
$url = $_GET['url'];
echo var_dump($url);
But all I get for $url is:NULL NULL NULL NULL NULL NULL

Edit: adjusted to handle both the redirect and the rewrite.
RewriteEngine On
RewriteBase /
# Redirect .php URLs to rewritten URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.php$ $1 [L,QSA,R=301]
# Rewrite URLs for processing by router (index.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L,NC]
You should exclude the RewriteCond %{REQUEST_FILENAME} !-d condition, as it will attempt to access a directory if your URL matches it. Probably not desirable when doing url rewriting.
index.php
$url = isset($_GET['url']) ? $_GET['url'] : null;
var_dump($url);

I just wanted to leave this somewhere for future users. This removes both HTML and PHP extensions from URLS.
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

Related

How to remove .php extension using htaccess

I have .htaccess file and inside it I have this code:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule (.*) /cvsrrc/index.php [L]
</IfModule>
I tried to add this which removes the .php extension but, I got 404'd.
RewriteRule ^(.*)$ $1.php
What I want to achieve is to add some code on my htaccess to remove the .php extension without removing the existing code above because I used that for my angular route. Thanks!
Try this. Got this from my project:
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
you can try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
edit:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
As #starkeen says, just add the following line into my .htaccess code. And here it is :
Options +Multiviews
This one works for me
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

trouble matching pattern against querystring for mod_rewrite in .htaccess file

I have 4 urls in my project that I am trying to translate using mod_rewrite
hall/description_banquethall.php?hall_id=1 > hall/description_banquethall/1
hall/venues.php?city=hyderabad&city_value=1&lacality=malakpet&locality_value=82 > hall/venues/hyderabad/1/malakpet/82
hall/hall_booking.php?hall_id=1&booking_date=2014-10-23&session=Morning > hall/hall_booking/1/2014-10-23/Morning
hall/booking_message.php?booking_id=10 > hall/booking_message/10
So far, I have the following in .htaccess which works for
hall/booking.php?hall_id=5&booking_date=2014-10-23&session=Morning
and
hall/description_banquethall.php?hall_id=5
but not for the two URLs listed above:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall/
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?city=([^&\s]+)&city_value=([^&\s]+)&locality=([^&\s]+)&locality_value=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?city=$2&city_value=$3&locality=$4&locality_value=$5 [L,QSA]
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?booking_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?booking_id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?booking_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
Why don't u use this in your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule
^(.*)/?$ index.php?page=$1 [L]
</IfModule>
Then in your index.php you return the appropriate page:
# SELECT WHICH PAGE TO DISPLAY
function getPage($showPage) {
$modules = array(#never name the slugs as same as the php pages
'/sign-in/i' => 'register.php',
'/signout/i' => 'logout.php',
'/staff/i' => 'admin.php',
'/products/i' => 'blank.php',
);
foreach ($modules as $slug=>$phpmodule) {
if(preg_match($slug, $showPage)) {
return $phpmodule;
}
}
return 'blank.php';#in case module not found display 404 page
}
if (isset($_GET['page'])) {
require_once (getPage($_GET['page'])); #include the appropriate module file
}

Original url: hall/booking.php?hall_id=1&booking_date=2014-10-23&session=Morning I want: hall/booking/1/2014-10-23/Morning

i have one url in my project like
hall/description_banquethall.php?hall_id=1 and my url is working like hall/description_banquethall/1
by using this .htaccess code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
now i want to redirect my another url hall/booking.php?hall_id=1&booking_date=2014-10-23&session=Morning to hall/booking/1/2014-10-23/Morning
Below ## hide .php extension snippet line add these 2 new rules:
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]

PHP redirect stop working after mod_rewrite (.htaccess) file

My redirect logic has stopped working ever since I've put the following code
Options -Indexes
ErrorDocument 404 /site3/public/admin/filenotfound.php
RewriteEngine On
RewriteBase /site3/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/picid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?search=([^&\s]+)\s [NC]
RewriteRule ^ %1/search/%2/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
this is how i'm redirecting my page
function redirectTO($url = null){
if($url != null){
header("Location:{$url}");
exit();
}
}
redirectTO('page.php');
I'm not a very good of mode rewriting therefore i cannot work out where the issue is please advise me what should, by the way they work fine if remove my .htaccess file doesn't work if put that file back on.
Any Idea?
use full path of your of the URL i.e. www.example.com/page.php
Since you're removing .php extension using rewrite rules and adding a trailing slash use this PHP code to redirect:
function redirectTO($url = null){
if($url != null){
header("Location: /site3/public/admin/" . $url);
exit();
}
}
redirectTO('page/');

Mode Rewrite added another url parameter but not working (multiple parameters)

I have the following htaccess file code below
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
This works perfectly fine as long as I provide 1 parameter which is caseid i.e
http://localhost/site/public/admin/PIClaimant.php?caseid=11
The URL above successfully changes to
http://localhost/site/public/admin/PIClaimant/caseid/11/
I've had a few pages with two parameters, which are PIClaimant.php?caseid=11&picid=34. Initially when I was creating htaccess file I did not pay attention to those pages (&picid=34 only being used at a few pages 4 or 5 max). Now when I go back to those pages which has those parameters, they are not working not showing the content of page. They work till first parameter PIClaimant/caseid/11/ but not with the second parameter.
Since I am not very good at this (mode rewriting) I need your help.
Any Ideas?
Things are getting more complicated now :)
Try this code in your .htaccess:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/picid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Change your code to the code below:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
replace the code with your code see if that works

Categories