How to remove www and index.php in url using .htaccess

Discussion in 'PHP' started by Ali_GGL, Dec 10, 2009.

  1. #1
    Hi i need a little bit of help.
    I have three requirements first one is to fix the page name with extension and second is variable name fixing and third is the www because i want to make my URL more short like below url

    OLD URL:
    http://www.globalguideline.com/index.php?JScript=first_JavaScript

    NEW URL (Required):
    http://globalguideline.com/first_JavaScript

    Please guide me,
    Regards,
    Hussain
     
    Ali_GGL, Dec 10, 2009 IP
  2. mahfuzit

    mahfuzit Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If you do not type www at the beginning of your url your site will appear without any problem. Then your next problm is index.php.

    To fix this problem follow my instruction.
    According your url if i am right you used get method. So in action attribute give the value "/"

    <form action = "/" method = get>

    I hope it will work.

    Then your url will be : http://globalguideline.com/?first_JavaScript
     
    mahfuzit, Dec 10, 2009 IP
  3. kingsoflegend

    kingsoflegend Well-Known Member

    Messages:
    202
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    108
    #3
    You can make the index.php part disappear with mod_rewrite.

    I think you can set an A record on the domain name to make the www go away. Record host: www.yourdomain.com, Record answer: yourdomain.com.
     
    kingsoflegend, Dec 10, 2009 IP
  4. lunarleviathan

    lunarleviathan Peon

    Messages:
    10
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I wouldn't try and go for a URL like that. The problem is that in the URL you want, there is nothing to say that the value after the / is. If you want to rewrite that exact URL:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*) index.php?JScript=$1

    But, you should aim for something like: http://domain.tld/page/var1/val1/var2/val2/var3/val3/ which is easily readable and search engine friendly and a lot more versatile in terms of multiple pages and GET variables. You could achieve that by:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^(.*)/(.*)/(.*)/ $1.php?$2=$3

    Which would rewrite the URL http://domain.tld/index/var1/val1/ to http://domain.tld/index.php?var1=val1 for processing by the server. You just then add another rule for each extra GET variable you need and add two more regex matches on to the end for the name and value.
     
    lunarleviathan, Dec 10, 2009 IP
  5. Wogan

    Wogan Peon

    Messages:
    81
    Likes Received:
    3
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Depends on whether or not you're running Apache 1.3.x or 2.x. I use the following .htaccess rule to handle all my redirects:

    RewriteEngine on
    RewriteCond $1 !^(index\.php|static|robots\.txt)
    RewriteRule ^(.*)$ /index.php/$1 [L]
    Code (markup):
    That'll redirect everything except index.php, static and robots.txt, but it doesn't deal with www. - that's what I use Apache's native ServerAlias directive for:

    <VirtualHost *:80>
    ServerName myserver.com
    ServerAlias www.myserver.com
    DocumentRoot /path/to/wherever

    AllowOverride All
    </VirtualHost>

    That'll redirect www. to the non-www variety in the background. AllowOverride is necessary in order for RewriteEngine to function within .htaccess.
     
    Wogan, Dec 11, 2009 IP