Search code examples
phpapache.htaccessbitnami

URL routing with htaccess


I am currently learning PHP and I have been attempting to make a PHP MVC website. However I am unable to get the URL routing to work correctly in my config file.

Here is my bitnami httpd-vhosts.conf file (as I am developing this on a bitnami server):

<VirtualHost *:443>
  ServerName dev.example.com
  DocumentRoot "/opt/bitnami/apps/dev/htdocs"
  SSLEngine on
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
  RewriteRule . /index.php?url=%{REQUEST_URI} [L,QSA]
  SSLCertificateFile "/opt/bitnami/apps/dev/conf/server.crt"
  SSLCertificateKeyFile "/opt/bitnami/apps/dev/conf/server.key"
  Include "/opt/bitnami/apps/dev/conf/httpd-app.conf"
</VirtualHost>

Based on my understanding this is what I am expecting this to do. Direct this URL:

https://dev.example.com/contact

to:

https://dev.example.com/index.php?url=contact

However it always takes my to this URL

https://dev.example.com/index.php?url=/contact

Any help would be greatly appreciated as I have been struggling with this issue for a few hours.


Solution

  • Bitnami Engineer here.

    If you want to remove the / of the URL, you only need to make a small change in the @Karkouch solution. Use this configuration line

    RewriteRule "^/?(.*)" /index.php?url=$1 [NC,L,QSA]
    

    And restart Apache after that :)