Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Mar 2012
    Posts
    11
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Question Hiding .php extensions

    All of my web pages use php. When people visit my website I don't want the .php file extension to appear in their address bar.

    I've been trying to figure out a way to remove or hide these file extensions. I've come across something called '.htaccess' but have had no success with that so far. I've tried simply removing the .php file extension from all of my relative links, and whilst this seems to solve the problem when visiting my website in a web browser, it breaks the website in IIS (Windows 8), which I use regularly.

    Would someone please be able to provide a solution which works both in web browsers and in IIS. I am a complete novice with this so apologies in advance.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    IF you're using IIS forget .htaccess files. They can be configured to work with IIS but this must be done by the server admin and most don't want to know. That means the default setup won't work with them.

    The only other way to hide your .php extension is to call your files index.php and then put them in unique directories such as:

    /register/
    /account/
    /profile/

    etc but leave out the index.php part. Sure, anyone can type index.php and successfully pull a page however it will obfuscate this a bit for you.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    New Coder
    Join Date
    Jan 2011
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by 553311 View Post
    All of my web pages use php. When people visit my website I don't want the .php file extension to appear in their address bar.

    I've been trying to figure out a way to remove or hide these file extensions. I've come across something called '.htaccess' but have had no success with that so far. I've tried simply removing the .php file extension from all of my relative links, and whilst this seems to solve the problem when visiting my website in a web browser, it breaks the website in IIS (Windows 8), which I use regularly.

    Would someone please be able to provide a solution which works both in web browsers and in IIS. I am a complete novice with this so apologies in advance.
    I think you can do it via .htaccess and PHP. All you really need to do is use .htaccess, but at the same time. If people know that it's ran by PHP, they can try to access it through like let's say index.php instead of index. But most people aren't really that smart or they wouldn't really go through all that trouble to type that into their address bar.

    .htaccess
    Code:
    RewriteEngine on
    RewriteBase /
    RewriteRule ^index$ index.php
    Then in index.php Just have this in the beginning code.

    PHP Code:
    <?php
    if($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'] == 'yourdomain.com/whateverthefileis.php') {
        
    header("Location: http://yourdomain.com/whateverthefileis");
    }
    ?>

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    Does IIS support .htaccess natively then? MY site is hosted by IIS and I can't use .htaccess
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #5
    New Coder
    Join Date
    Jan 2011
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tangoforce View Post
    Does IIS support .htaccess natively then? MY site is hosted by IIS and I can't use .htaccess
    I have no clue. I bought my site on cheap-domainregistration.com. They support .htaccess and most of everything other domain registers have.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    This is clearly turning into a topic of debate and I've received a PM about this today from a member who I won't name as they should have picked up on this in my previous post and really deserve a public flogging :

    Quote Originally Posted by annonymous
    Hi tangoforce

    ref: Hiding .php extensions

    Hope you don't mind the pm. I decided to pm rather than post because I thought maybe you could investigate this and if it's workable you could post a solution and if it's not you might tell me why so I can dismiss it.

    Apparantly .php extention can be changed to anything eg .html. asp etc

    Quote from my book:

    Just change the line in your httpd.conf file that reads

    AddType application/x-httpd-php .php

    to whatever extension you please, such as

    AddType application/x-httpd- .asp

    Of course, you'll need ot eb sure that this does not cause conflict with other installed server technologies.
    First of all
    I don't personally understand this as I have never tried to do it so I have no idea if this is practical in the 'real world'. Sounds too easy, must be a catch.

    You have a good understanding of php and how things work, maybe you can say whether this is workable or not.

    Annon
    You are correct in that .htaccess will allow this (and I've used it many times myself) but the op has said:

    Quote Originally Posted by 553311 View Post
    Would someone please be able to provide a solution which works both in web browsers and in IIS.
    IIS is microsofts http server not the http server from the apache foundation found on linux systems. There is a misconception that .htaccess is used by all webservers due to some sort of standard. I'm afraid I have big news for you: It's not! The http protocol is the standard webservers work to but how they do it and store their own configurations is not part of it is down to the authors.

    .htaccess files are apache configuration files and IIS doesn't natively support them (AFAIK).

    You can get them to work using ISAPI_Rewrite (which comes directly from the IIS.net forums but thats a paid addition to IIS not a native part of it. You'd also need the server admin to install it.

    IIS uses it's own configuration files that are in XML format and to be frank, it's horrible to use / look at / try to understand. There may be a IIS config program out there on google that makes them easy to generate but I'm afraid I never looked for one as I came up with another workaround for my problem at the time (which was totally different to the ops problem).

    Apache became popular as it was basically written by nerds for nerds and was a skeleton web server that could use many different custom modules (hence it was named Apache because it was "a patchy web server". With the huge number of additional modules and advanced configurations it could support it was only natural that it would become the most popular when offered on the free linux OS.

    IIS however is a commercial offering from Microsoft who charge for it (either up front or hidden in the OS cost). While commercial usually means lots of advanced features it doesn't mean it will work in an identical manner to the free alternatives or even have the same features.

    You can run cron on linux but if you tried on windows you'd be very disappointed because it's windows task scheduler and that works differently but does the same thing in a different manner
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #7
    New Coder
    Join Date
    Nov 2012
    Location
    United Kingdom
    Posts
    39
    Thanks
    3
    Thanked 1 Time in 1 Post
    Quote Originally Posted by tangoforce View Post
    Does IIS support .htaccess natively then? MY site is hosted by IIS and I can't use .htaccess
    Yes: http://www.helicontech.com/ape/

  • #8
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,329
    Thanks
    60
    Thanked 526 Times in 513 Posts
    Blog Entries
    4
    No thats a 3rd party addon not a NATIVE feature of IIS and the op can't install that himself it would need the server admin to do so.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •