Score:0

Apache 2.4 Redirect all to index.php

cn flag

OK, first let me apologize if this question has been asked before. I did a search but didn't immediately find something suitable.

I've been looking at the Apache docs but just cannot get this rewrite to work, though it seems it should be very simple, so I must be missing something simple.

My vhost conf file looks like this:

<VirtualHost *:80>
    ServerName xxxxx.local
    DocumentRoot /var/www/html/configManager
    DirectoryIndex index.php

    <Directory /var/www/html/configManager>
        AllowOverride All
        Order Allow,Deny
        Allow from All

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^.*$ index.php [QSA,L]
        </IfModule>

    </Directory>
</VirtualHost>

To me that reads: redirect everything, capture nothing, to index.php But if I go to http://xxxxx.local/Listmanager/view/5 I get a 404. For anything other than "/" I get a 404.

I've tried adding an Alias, using mod_dir instead of mod_rewrite, but the best I get is a 403 or a 404.

As you can probable see from the URL, I'm trying to code this application MVC style, but it looks like the server is always looking for a filesystem path instead of just passing everything.

Any help would be greatly appreciated.

Score:1
ru flag

This is the current and correct way to configure HTTPD for what you ask:

<VirtualHost *:80>
    ServerName xxxxx.local
    DocumentRoot /var/www/html/configManager
    DirectoryIndex index.php
    FallbackResource /index.php

    <Directory /var/www/html/configManager>
        Require all granted
        Options none 
        AllowOverride none 
    </Directory>
</VirtualHost>

Notes:

FallbackResource is a mod_dir directive to point everything that do not exist back to a single controller page. In 2023 this is the way to do it instead of the insidiously old and convoluted: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^.*$ index.php [QSA,L] # QSA does not need to be added here, Apache will add them automatically when you do not specify query string to target. It is better to not copy paste.

Remember to not use Allow,Deny,Satisfy, they are deprecated old 2.2 directives and unload mod_access_compat which is a module used for compatibility only with old directives, not meant to extend the usage of deprecated ways.

About: "Options none" If apache complains you may have to add +FollowSymlinks, depends on if you add mod_rewrite directives later on, but in most simple cases this is the way to go.

About "AllowOverride none" Do not use .htaccess unless you want to allow non-admin users config rights to certain directories, the more "per-dir" rewrites and such you add, the more complicated and convoluted your configuration will be, so whenever possible stick to configuring redirects/rewrites and such in virtualhost context. If all configuration you need is simple redirects, I would suggest you to check mod_alias (Redirect, RedirectMatch) instead of mod_rewrite (which is a swiss knife).

Score:0
cn flag

Oh, kill me sideways with a trash can! Module was not enabled!

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.