Hafidz Ikhsan Baihaki
Hafidz Ikhsan Baihaki

Reputation: 11

Set .htaccess at localhost and hide index.php

I wanna ask about .htaccess that could be change the url to be more simple. I have the url in my localhost like this:

localhost/spsb/index.php

and

localhost/spsb/index.php?page=student

The question is:

  1. How can i change the url above being:

localhost/spsb

and

localhost/spsb/student
  1. And where is the correct place to save the .htaccess file?

Thank's a lot for advice I have been searching a lot everywhere but no one can solve my problem

Upvotes: 0

Views: 129

Answers (1)

MTroy
MTroy

Reputation: 895

Use URL Rewriting

For an Apache server:

First, you must activate the rewrite module (How to active mod_rewrite)

Add those lines to your htaccess to enable rewrite engine

Options +FollowSymlinks

RewriteEngine On

And this line to rewrite the url as you want

RewriteRule ^spsb\/?$ spsb/index.php
RewriteRule ^spsb/([a-zA-Z]+)$  spsb/index.php?page=$1  [L]

For a Nginx server:

Put these lines in "Location" scope:

if (!-e $request_filename)
{
    rewrite ^spsb\/(.*)$ spsb/index.php?page=$1;
}

Note: the location of .htaccess is relative to his level into the site structure, if he is inside the first level site/dir/ the contained rules are limited to site/dir structure

To be unlimited, i suggest to put it into the document root ...Or you can put the rules directly into you vhost configuration

How to check errors:

  • First have you restarted your server to apply the activated rewrite modules
    (rules from htaccess dont require a restart, only module modification)
  • Add following lines to your htaccess after "RewriteEngine On":

    RewriteLog "/var/log/apache2/{your_log_site...}" <-- put your path

    RewriteLogLevel 7

  • Check by a "tail -f /var/log/apache2/you_log_site.errorlog" to see if an error appear at resfresh page

Upvotes: 1

Related Questions