Redirect all requests to index.php

11,463

Here is how you create an alias and route everything to index.php inside aliased directory.

Alias /minus_project /var/www/minus_project

<Directory /var/www/minus_project>
   Options Indexes FollowSymLinks MultiViews ExecCGI
   AllowOverride All
   Order allow,deny
   Allow from all
   Require all granted

   RewriteEngine On
   RewriteBase /minus_project/

   RewriteRule ^/index\.php$ - [L,NC]

   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule . index.php [L]
</Directory>

Place above snippet inside VirtualHost section and don't forget to restart Apache server.

Share:
11,463
Vahag Chakhoyan
Author by

Vahag Chakhoyan

Updated on June 27, 2022

Comments

  • Vahag Chakhoyan
    Vahag Chakhoyan almost 2 years

    I have a root folder /var/www/minus_project and only two files in it: index.php and .htaccess.

    How to make apache2 redirect all requests like localhost.com/minus_project/some/url/here/... to index.php? My apache2 configuration

    <VirtualHost *:80>
            ServerAdmin [email protected]
            DocumentRoot /var/www/html
            Alias /phpinfo /var/www/phpinfo
            Alias /minus_project /var/www/minus_project
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    </VirtualHost>
    
  • Álvaro González
    Álvaro González over 7 years
    Please note that <Directory> containers are only valid (or actually necessary) in main .conf files, not in .htaccess files. The question is kind of vague about it.
  • anubhava
    anubhava over 7 years
    True, even alias cannot be used in .htaccess
  • Vahag Chakhoyan
    Vahag Chakhoyan over 7 years
    All right, except I have run sudo a2enmod rewrite command before restarting apache.