2

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]
1
  • Your question answer are on here.. Commented Mar 30, 2014 at 4:54

3 Answers 3

3

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".

Sign up to request clarification or add additional context in comments.

5 Comments

+1 for making sure that the filename can (nearly) only be alphanumeric, as opposed to "Try This"
@AustinBurk thank you for your comment but check 3rd line rules a-zA-Z0-9 so its work not only alphabetic but also numeric url work fine. unfortunately i'm not try this in my existing url so i'm not give you surety.
@AlexPelletier thank you for testing numberic url not work. alphanumeric work great. oky i will create regexp for supporting numeric and alphabetic both are work.
all of my domains alphanumeric though? I will test again. Tested and i get my 404 page
@user1153551 What I meant as 'nearly alphanumeric' was that it include hyphens as well as a-z,A-z, and 0-9. Again, regex is not my forte, I'm probably wrong
2

try this

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

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

5 Comments

Care to explain to OP what this will do?
This does work, but I would love to see how this work?
It takes the directory name as an argument /example and simply tries to run a PHP file with that name. example.php I feel like this could lead to to directory traversal, that regex may match any name (take that with a grain of salt, regex isn't my forte.) . What about "example.com/ls ~ && index"?
if example.com/sample is entered it searches for example.com/sample.php (it is case insensitive). If a folder exist in that directory is called example2, avoid naming a file example2.php, else if example.com/example2 is entered, it will fetch example.com/example2/
@AlexPelletier if it solves your problem, mark it as your answer
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.