Vishnu Vishnu - 6 days ago 6
PHP Question

Rewriting page urls in website

I created a simple website and some of the page urls are having the pattern "*.php".

I want to make my url

/home.php
as
/home
. Can I do this in
.htaccess
file?

I am adding my code here

<files campus_config.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<files student.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<FilesMatch (variables|mysql)\.php>
order allow,deny
deny from all
</FilesMatch>

RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]


sample url:
localhost/test/campus/login.php (test is a folder ,campus is my project folder ,.htaccess file is in my project folder)

Answer

Place this line on in your /test/campus/.htaccess:

RewriteEngine on
RewriteBase /test/campus/

RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]
Comments