wanted to hide .php extention in url

Discussion in 'General PHP' started by kslakhani, Apr 29, 2015.

  1. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    I want to hide .php extention from my webid url i tried

    # Make PHP code look like unknown types
    AddType application/x-httpd-php .do
    no success
    then

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    no success
    followed this not working either.
    my host provide full .htaccess support and its php 5.+
    Am i missing something???
     
  2. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Hi kslakhani, this works for me.
    Code:
    RewriteEngine on
    # REMOVE PHP EXTENSION
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    Do you want to include trailing slash? replace RewriteRule with the below
    RewriteRule ^(.*?)/?$ $1.php [L]
     
    kslakhani likes this.
  3. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    500 Error, please check your php script / enable display_errors in your cpanel
    display_errors already on in cpanel:(
     
  4. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    my ,htaccess looks like
    Code:
    php_value display_errors On
    php_flag magic_quotes 1
    php_flag magic_quotes_gpc 1
    php_value mbstring.http_input auto
    php_value date.timezone America/New_York
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^XXXX\.com$ [NC]
    RewriteRule !^(admin.*|auctions.*|abstrap.*)$ http://www.XXXX.com/$1 [R=301,L]
    
    RewriteEngine on
    # REMOVE PHP EXTENSION
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    
    # Prevent Direct Access to files
    <FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
    Order deny,allow
    Deny from all
    </FilesMatch>
    Options -Indexes
    AddDefaultCharset UTF-8
    AddCharset UTF-8 .tpl
    AddCharset UTF-8 .js
    AddCharset UTF-8 .css
    AddCharset UTF-8 .php
    
     
  5. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Try :
    Only use ' RewriteEngine on ' once, (before the first rewrite begins).
    and place the code
    Code:
    # REMOVE PHP EXTENSION
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    above you other rewrite code so it looks like

    Code:
    RewriteEngine on
    
    # REMOVE PHP EXTENSION
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteCond %{HTTP_HOST} ^XXXX\.com$ [NC]
    RewriteRule !^(admin.*|auctions.*|abstrap.*)$ http://www.XXXX.com/$1 [R=301,L]
     
  6. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    still not working:mad:
    Code:
    # REMOVE PHP EXTENSION
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    
    RewriteCond %{HTTP_HOST} ^xxx\.com$ [NC]
    RewriteRule !^(admin.*|auctions.*|abstrap.*)$ http://www.xxx.com/$1 [R=301,L]
    
    
    # Prevent Direct Access to files
    <FilesMatch "(?i)((\.tpl|\.ini|\.log|(?<!robots)\.txt))">
    Order deny,allow
    Deny from all
    </FilesMatch>
    Options -Indexes
    AddDefaultCharset UTF-8
    AddCharset UTF-8 .tpl
    AddCharset UTF-8 .js
    AddCharset UTF-8 .css
    AddCharset UTF-8 .php
     
  7. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Does it work without
    Code:
    RewriteCond %{HTTP_HOST} ^xxx\.com$ [NC]
    RewriteRule !^(admin.*|auctions.*|abstrap.*)$ http://www.xxx.com/$1 [R=301,L]
    You should be able to enter wwwdotyour_webid/index in the browser and it should take you to index.php and not a 404 page.
    I know the re writes can be tricky to set up and don't work the same on all the hosts
     
  8. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    tried the above one, no success
    this code works
    Code:
    RewriteCond %{HTTP_HOST} ^xxx\.com$ [NC]
    RewriteRule !^(admin.*|auctions.*|abstrap.*)$ http://www.xxx.com/$1 [R=301,L]
    I have installed webid on my htdocs/auctions, so if i write mysite.com/auctions it opens index.php without showing index.php but then if i press home it shows mysite/auctions/index.php?
    Weired innit
     
  9. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    mysite.com/auctions is a folder, mod-rewrite will guide it to an index.php, or if not found to an index.html in the folder.
    When you press home link in webid it is coded as href="index.php" so that will take you to exactly that.
    If you guide it (in the browser) to mysite.com/auctions/index it will throw an error as index folder does not exist.
    With the htaccess mod we tell it if index (or anything else) is not a folder try first to find index.php and then if not found throw an error.
    If you add the mod rewrite, try mysite.com/auctions/index in the browser. If it shows the index.php (home) page it works.
    Then what you need to do is go and change ALL the links in all the pages to href="index"
    Obviously the same applies for all the other links. Then when you press the home link it will take you to mysite.com/auctions/index
    Any links with href="index.php" will always take you to index.php
     
  10. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    i tried mysite.com/auctions/index it opens the homepage without showing index.php
     
  11. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    Then it works. The next step is the painful step. You need to go through all your pages and remove all the .php links from all the href's.
    If there are no index.php links on your site it will never go to an index.php address:)
     
  12. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    any other way other than above step:D
     
  13. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    I know it is frightening. The good thing is there is no rush as any index.php files will still work fine but will show as index.php.
    I think I know what you are thinking now - There should be an easy way when I click index.php to be redirected to index.
    I don't know myself an easier way without a main router. I have looked into this but a few years back. It might be possible but could involve an additional redirect which would not be good for seo. Maybe someone else could help if they know an easier way
     
    kslakhani likes this.
  14. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    Pani100, thanks buddy tht working now.
    I know its tedious.
    I only wanted to hide for security reason so hackers dont know in what lang is used for me site.
    will it affect to search engines???
    what would you recommend?
     
  15. pani100

    pani100 Well-Known Member

    Joined:
    May 9, 2011
    Messages:
    2,327
    Likes Received:
    449
    As with all seo pages which most of the times can be accessed under different names you would need to add a canonical link.
    This tells the search engines what page to record and use and not see the other content as duplicate and get penalised
    Just google it.
    You would be adding this to the header for the index page : <link rel="canonical" href="http://mysite.com/auctions/index"/>
    similar for other pages.
     
    kslakhani likes this.
  16. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    thanks a lot, really appreciated
     
  17. david62311

    david62311 Well-Known Member

    Joined:
    Aug 29, 2013
    Messages:
    2,133
    Likes Received:
    242
    This too might work:

    Code:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule ^(.*)$ $1\.php
     
    kslakhani likes this.
  18. kslakhani

    kslakhani Active Member

    Joined:
    Mar 12, 2015
    Messages:
    128
    Likes Received:
    15
    david62311 likes this.

Share This Page