SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to hide file name and .extention using htaccess

    Hi , I have two urls. I have written them below

    1.<a href=”index.php?software=ture”>Software</a>
    2.<a href=”index.php?contact=ture”>Contact</a>

    What i want to do is that I need to remove the file name and .extention from both of the urls. I am using wamp server on windows xp.
    My url should be like this in the addressbar,
    1.Mydomain/?software=ture.
    2.Mydomain/?contact=ture .

    I have heared about htaccess. But I don’t know how to do that.
    I have witten my full code below.

    HTML Code:
      [PHP]
      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    <style type="text/css">
    <!--
    .container {
    	margin: auto;
    	width: 960px;
    }
    
    .bg {
    	margin: auto;
    	width: 960px;
    }
    .bg .wrapper {
    	background-color: #FFCCFF;
    }
    -->
    </style>
    
    </head>
    
    <body>
    <div class="container">
    <div class="bg">
      <div class="wrapper">
      	<a href="index.php?page=software">Softwares</a> | <a href="index.php?page2=contact">Contact</a>
      </div>
      
    </div>
    
    	<?php 
    		$software = $_GET['page'];
    		$contact = $_GET['page2'];
    		if($software)
    		{
    			include("software/software.php");
    		}
    		else if($contact)
    		{
    			include("contact/contact.php");
    		}
    	?>
    </div>
    </body>
    </html>
    
    [/PHP]
    Please any body help me.

    Thanks in advance.

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi
    To rewrite te url, you need .htaccess with mod-rewrite.
    Try this code in .htaccess:
    Code:
    Options +FollowSymLinks
    RewriteEngine On
    
    # Incepe comanda pt. mod-rewrite
    
    RewriteRule ^?([a-zA-Z0-9=_-]+)$ index.php?$1 [NC,L]
    Also, rewrite the new url in your php code.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Member
    Join Date
    May 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, I tried the below url.

    Code:
    Options +FollowSymLinks
    RewriteEngine On
    
    # Incepe comanda pt. mod-rewrite
    
    RewriteRule ^?([a-zA-Z0-9=_-]+)$ index.php?$1 [NC,L]

    I got some errors like..

    Code:
    Internal Server Error
    
    The server encountered an internal error or misconfiguration and was unable to complete your request.
    
    Please contact the server administrator, admin@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
    
    More information about this error may be available in the server error log.
    I couldn't run the code successfully.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try with this code:
    Code:
    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^/?$ /index.php [QSA,L]
    Or someone more experienced can help.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  5. #5
    SitePoint Member
    Join Date
    May 2012
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi i tried the below code.

    Code:
     Options +FollowSymLinks
    RewriteEngine On
    
    RewriteRule ^/?$ /index.php [QSA,L]
    it works. but i couldn't hide index.php in the addressbar

    when i click my anchar tag it displays url like "http://localhost/myfolder/index.php?page=true" in the addressbar.

    what i need is that the url should be like http://localhost/myfolder/?page=true when i click the anchar tag.

  6. #6
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Replace the link in the anchor tab too.
    If not works, i not know other way, maybe someone else.
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,671
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Some hosts configure their servers to NOT serve the / but they're relatively rare.

    My advice is to LINK to /?querystring (although I hate that because it forces Apache to go find the first DirectoryIndex and serve that) as your link rather than try to force your server to remove the name of the file it's to serve.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Tags for this Thread

Bookmarks

Posting Permissions

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