how to hide page extensions

Dear Experts,
I'm using PHP
I know that some websites use php, but they do not have page extentions like about.php
Do they use folders for every page or do they use something to hide their page extentions?
I click the link, the link is www.domain.com/about-us
do they create about-us folder for every page they have or do they use something for the SEO purposes?
LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
There are several ways to do this depending on your setup. For example, if you're using something like an MVC Framework, then you never actually access a page - you go through a controller.

If you're just using standard PHP files, then you usually do it by adding or editing an .htaccess file in the root of your site. This allows you to use the mod_rewrite function to automatically remove the extension. The content of the file may vary depending on your hosting setup but it would end up looking something like this:

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

Open in new window

Then when you type /about-us into your browser, the mode_rewrite would know to actually grab the about_us.php file
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Olaf DoschkeSoftware DeveloperCommented:
Just let me add you can also use mod_rewrite (the RewriteEngine) to call a controller and make this "folder name" (a term for that is slug) into a parameter.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$        /$1/ [R=301,L]
RewriteRule ^$                    controller.php?detail=home [NC,L] #handle requests into the root domain as default detail "home"
RewriteRule ^([a-z,0-9,\-]+)/$           controller.php?detail=$1 [NC,L] #handle requests with specified detail (eg about-us)

Open in new window


This routes any request which doesn't have an explicit file extension to a controller.php script forwarding that "folder name" as a parameter named "detail". It's a matter of taste how you call this, might also be page or topic.

There's more you can do, and it pays to dig into what the rewrite engine is capable of. This is very specific to Apache, though. URL rewriting works very different for IIS as web server. For international sites not as big as having all country/language specific domains, you may use a URL schema of domain.com/lang/topic and/or you might also "encode" the usage of which controller is used, instead of making use of subdomains for a blog part, the blog part of a website might be indicated by /blog/ inside the URL and then can be routed to another controller than the rest of the site.

Rewrite rules are also used to handle very basic things, like avoiding double addressing of the same URLs with www prefix or without, and you have the right idea this also is used to be SEO friendly without actually having such a folder structure on the web server.

Bye, Olaf.
1
Jim RiddlesPrepress/OMS SpecialistCommented:
If you are using Apache to serve your pages, you can also use MultiViews.

http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews

Per that page:
The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.
Be aware that using MultiViews with mod_rewrite can cause undesired results.

An example in a VHOSTS file would look like the following:
<VirtualHost *:80>
        ServerAdmin email@host.com
        ServerName www.example.com

        DocumentRoot /var/virtualhosts/example.com/www
        <Directory />
                Require all granted
                Options FollowSymLinks
                AllowOverride None
        </Directory>
        <Directory /var/virtualhosts/example.com/www/>
                Require all granted
                Options -Indexes +FollowSymLinks +MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog ${APACHE_LOG_DIR}/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel crit
        CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Open in new window

With this example, if you have a file named about.php in the root folder, you could access it by browsing to http://www.example.com/about.  Of course, if you have a file named about.html, it would be up to the server to decide which file to serve.

MOD_REWRITE is probably a better route, overall, however if your needs are simple and you ensure you don't have duplicate filenames with different extensions, MultiViews can serve your purposes.
1
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

BRMarketingAuthor Commented:
Dear Experts,

My htaccess file is like this: I always use the https instead of http
to do this I modified my htaccess file like below.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Open in new window


in addition to this, now I want to hide the file extensions. So, Should I just merge these codes like this.
What do you suggest I should do? Thank you
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Open in new window

0
Chris StanyonWebDevCommented:
That looks like it should work fine. GIve it a try and come back if you have problems.
1
BRMarketingAuthor Commented:
Thank you all
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.