Alex Pelletier
Alex Pelletier

Reputation: 5123

How to edit .htaccess to remove .php

All of my pages end in .php, I need these pages to still be able to run PHP even though the extension has changed in the url bar. I want a page like

website.com/page.php

To

website.com/page

I have looked at half a dozen work arounds to this problem but none of them seem to work. Here are some examples.

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]

RewriteEngine On
RewriteRule \/([^\/]+)\/$ $1.php

# once per htaccess file
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-_]+)/?$ /$1.php
RewriteCond %{REQUEST_URI} !^/shopName1.php$
RewriteRule ^([a-z0-9-_]+).php$ /$1/ [R]
RewriteRule ^shops/shopName1/?$ /shopName1.php
RewriteRule ^shopName1.php$ /shops/shopName1/ [R]

Upvotes: 2

Views: 56

Answers (3)

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

Hide the .php Extension

RewriteEngine On
RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php

Explanation How this work

Ans. This rule will match url.com/path/ and check, if url.com/path.php exists. If file exists then process ahead to rewrite rule apply.

Currently I used this rule Work perfect, Hope this help you!


Updated

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

in the above code the -d define for Directory and -f for Regular File.

This rule will same as above but its support Alphanumeric URL. The above code is Tested and Work Fine with my server files.

Example: If your filename is "www.url.com/path1.php" then your may access this file directly with "www.url.com/path1".

Upvotes: 3

Austin Burk
Austin Burk

Reputation: 960

Just make a directory called page and rename page.php as index.php and put in in /page. If you need dynamic directories (like /product5 runs details.php?productid=5), then an .htaccess solution would be useful.

Upvotes: 0

BlackPearl
BlackPearl

Reputation: 2775

try this

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

Upvotes: 2

Related Questions