How to remove .php extension from URL

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samatair
    New Member
    • Nov 2007
    • 61

    How to remove .php extension from URL

    Hi
    I need to remove the .php extension using .htaccess from my URL.
    I have tried different .htaccess code by searching in the net but none is fruitful..

    I have the least idea about regular expressions. can anyone please help?

    my URL is
    http:///www.domainname.com/projectna...p/region/state

    here region and state are dynamically changing... and i want to remove .php from filename.php
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    what does your .htaccess code currently look like?

    Comment

    • Markus
      Recognized Expert Expert
      • Jun 2007
      • 6092

      #3
      Say your .htaccess file is in /projectname/. Try the following:

      Code:
      # Start the engine. Vroooom.
      RewriteEngine On
      
      # This resolves paths to the current directory. (You may or may not need this).
      RewriteBase /projectname/
      
      # And the rule.
      RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]
      The (untested) rewrite basically takes everything after the RewriteBase, up to the first forward slash (if there is one) and stores everything in variables. We then rewrite the url, adding '.php' to the first capture ($1), appending the second capture ($2) to the URL too.

      Let me know if this helps.

      Comment

      • n8kindt
        New Member
        • Mar 2008
        • 221

        #4
        you could also tell php to parse a file with your own custom file extension. for instance in your case you could do



        by putting something like this into your .htaccess

        Code:
        #tell php to read all files with ".name" as the file extension
        AddHandler application/x-httpd-php5 .name
        that way it would look like the "dot" between file and name was on purpose and have nothing to do with the language your code is written in

        Comment

        • samatair
          New Member
          • Nov 2007
          • 61

          #5
          Originally posted by Markus
          Say your .htaccess file is in /projectname/. Try the following:

          Code:
          # Start the engine. Vroooom.
          RewriteEngine On
          
          # This resolves paths to the current directory. (You may or may not need this).
          RewriteBase /projectname/
          
          # And the rule.
          RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]
          The (untested) rewrite basically takes everything after the RewriteBase, up to the first forward slash (if there is one) and stores everything in variables. We then rewrite the url, adding '.php' to the first capture ($1), appending the second capture ($2) to the URL too.

          Let me know if this helps.

          Thanks Markus for providing the code. But the snippet didn't work for me. when I removed the .php extension from the filename the page did not come up.
          But I think I can try my hand with what you have provided.

          Thank you very much!

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            It was untested (I was a little strained for time).

            Anyway, try this one:

            Code:
            RewriteRule ^([a-zA-Z0-9]+)(/.*)?$ /$1\.php$2 [R]
            Note: the [R] flag will rewrite the URL. That is, the URL will be visibly changed. Change it to [L] (or omit the flag altogether) if you want to URL to be 'masked'.

            Comment

            • Joe Smith

              #7
              Thanks Markus

              markus' first snippet worked for me (without the rewritebase part)

              Comment

              • selva kumar
                New Member
                • Oct 2010
                • 1

                #8
                For Me Its Not Working Markus

                Comment

                • Shaikh Farooque

                  #9
                  hey try this..... It is tested and working for hiding the .php extension...... .

                  Code:
                  RewriteEngine on
                  RewriteCond %{REQUEST_FILENAME} !-d
                  RewriteCond %{REQUEST_FILENAME}\.php -f
                  RewriteRule ^(.*)$ $1.php
                  my id is ** Edit - Removed **
                  Last edited by NeoPa; Nov 18 '10, 04:11 PM. Reason: Added CODE tags and removed email

                  Comment

                  • upenmishra
                    New Member
                    • Aug 2011
                    • 1

                    #10
                    thankyou so much man!!!!!!! Its perfect what i as looking for...

                    Comment

                    Working...