Score:0

Apache mod_rewrite will not redirect from root folder to subfolder

jm flag

I will preface this question by saying that I know next to nothing about Apache directives, but I need to use them in my project to hide the contents of the root folder, and also to hide the /public portion of the URL.

httpd.conf

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so

I know that mod_rewrite is enabled, because I have other .htaccess files in the same project that work just fine. It is just this one file in the root folder that seems to have no effect.

example of a .htaccess file in the /public folder that works

<IfModule mod_rewrite.c>
  Options -Multiviews
  RewriteEngine On
  RewriteBase /php_mvc/public
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule  ^(.+)$ index.php?url=$1 [QSA,L]
</IfModule>

.htaccess file that does not work

<IfModule mod_rewrite.c>
  RewriteEngine On RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L]
</IfModule>

(update) same .htaccess file as previously, but reformatted

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
</IfModule>

Are there any errors in my .htaccess file, or is there something that I have overlooked in my Apache configuration for this to work as intended?

Note that my intention is to redirect the visitor away from the root folder and into the public subfolder, and hide the /public portion of the URL, so that localhost/my_site/public would become localhost/my_site/.

I run Apache locally on macOS Monterey with virtual hosts enabled. I do not make use of the default directory, but instead have my project in another location.

httpd.conf

DocumentRoot "/Users/mihkel/Documents/development/www"
<Directory "/Users/mihkel/Documents/development/www">
    #
    # Possible values for the Options directive are "None", "All",
    # or any combination of:
    #   Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
    #
    # Note that "MultiViews" must be named *explicitly* --- "Options All"
    # doesn't give it to you.
    #
    # The Options directive is both complicated and important.  Please see
    # http://httpd.apache.org/docs/2.4/mod/core.html#options
    # for more information.
    #
    Options Indexes FollowSymLinks

    #
    # AllowOverride controls what directives may be placed in .htaccess files.
    # It can be "All", "None", or any combination of the keywords:
    #   AllowOverride FileInfo AuthConfig Limit
    #
    AllowOverride None

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>
kz flag
`AllowOverride None` - ??? "Are there any errors in my .htaccess file" - Is it literally all on one line like that? That is certainly an "error". "I have other .htaccess files in the same project that work just fine" - where?
Mihkel Pajunen avatar
jm flag
VSCode seems to auto-format what would otherwise be three lines into this one line. I tried to manually edit the .htaccess file so that it looks like the second version (see the update above).
Mihkel Pajunen avatar
jm flag
I have now added an example of a .htaccess file within the **public** folder (a direct subfolder of the **root** folder), that works as intended.
Mihkel Pajunen avatar
jm flag
It looks like the poor formatting was the culprit. After having manually formatted the contents of the .htaccess file to look like the updated example, and having restarted the Apache service, it works just as I had intended.
kz flag
Glad you got this working. However, that would mean you've posted an irrelevant (or incomplete) section of your server config? Is your `localhost` not pointing to `/Users/mihkel/Documents/development/www`? As per the "???" in my earlier comment, `AllowOverride None` completely disables the use of `.htaccess` files in that directory tree, so you must have something else that enables it again for this to work? Or this part of your config is not relevant to your question?
Score:1
jm flag

It seems like my editor, VSCode, tried to auto-format any .htaccess files placed directly within the root folder whenever I would save the file.

What should have been:

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteRule ^$ public/ [L]
  RewriteRule (.*) public/$1 [L]
</IfModule>

...instead, ended up as:

<IfModule mod_rewrite.c>
  RewriteEngine On RewriteRule ^$ public/ [L] RewriteRule (.*) public/$1 [L]
</IfModule>

This was not the case for any of the .htaccess files placed within a subfolder.

The Apache Conf extension for VSCode solves this issue, but one could always use a different editor altogether for just the .htaccess files.

kz flag
What "file type" is VSCode defaulting to for `.htaccess` files (without the "Apache Conf" extension)?
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.