.htaccess

how to remove Extensions from url

Use this .htaccess code here (the new htaccess file must go in the htdocs folder):

#Script for URL rewriting

#The line below may exist.
RewriteEngine On
#Rewrite all the rules, with / as the base, not requesting any file or folder.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#Replace $1.html with $1.php if you're running PHP scripts and want to remove the extensions to them:
RewriteRule ^([a-z]+)\/?$ $1.html [NC]

i have tried many times but not working

Are you using my script on the .htaccess file? If not use it!

yes i have use it
but no effect

Are the file extensions .php, .html or of other type, so I can correct the htaccess?

.php

it works for only index page not for others

I had written on a comment “Replace $1.html with $1.php if you are running PHP scripts”, but seem like you didn’t replace it!

its like work sometimes and sometimes not

Correcting the code on the fly!

#Script for URL rewriting

#The line below may exist.
RewriteEngine On
#Rewrite all the rules, with / as the base, not requesting any other file or folder.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([a-z]+)\/?$ $1.php [NC]
1 Like

thanks

1 Like

i’m wondering we’ve to send requests like this?

example?id=1

instead of

example.php?id=1

You can also request example.php?id=1 with my code.

1 Like

Are you sure about this regex?

RewriteRule ^([a-z]+)\/?$ $1.php [NC]

I didn’t test it, but looking at the regex, I would guess that this would only work for requests like http://example.com/page or http://example.com/page/ (both going to page.php). Since the regex only matches [a-z], if you have subdirectories, or files with capital letters, number or special characters, this regex would not be matched and the code wouldn’t do anything.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.