DEV Community

Fega Suseno
Fega Suseno

Posted on

Remove index.php in Apache URL Using .htaccess

Pada postingan kali ini kita akan bahas gimana sih caranya menghilangkan index.php saat akses url. Nah kalo di framework baru biasanya udah otomatis di hilangkan yaa. Ini memanfaatkan module rewrite di apache untuk mensettingnya. ok kita langsung ketutorialnya.

Pertama aktifkan mod_rewrite

Jalankan perintah berikut

sudo a2enmod rewrite
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Edit VirtualHost Apache, buka file konfigurasi biasanya ada di:
/etc/apache2/sites-available/domain.conf
atau default-nya:
/etc/apache2/sites-available/000-default.conf

Tambahkan konfigurasi AllowOverride All pada folder project:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/html/api

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

+        <Directory /var/www/html/api>
+            AllowOverride All
+            Require all granted
+        </Directory>

</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Simpan, lalu reload Apache:

systemctl reload apache2
Enter fullscreen mode Exit fullscreen mode

Tambahkan .htaccess di dalam folder root project (sebagai contoh /var/www/html/api). Jika belum ada, maka buatlah secara manual file .htaccess lalu isikan seperti berikut

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Enter fullscreen mode Exit fullscreen mode

lalu restart servicenya

systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Sekarang uji coba buka:

http://domain.com/index.php/tes
Harus bisa diakses dengan
http://domain.com/tes

Oke selamat mencoba dan semoga bermanfaat

Top comments (0)