Stievy Dude Stievy Dude - 16 days ago 5
Apache Configuration Question

.htaccess RewriteRule php redirect folder to another

I have a folder test. If a person navigates to the folder test, I want the browser to display new.

The folder new doesn't exist on the server, I just want it to be displayed as new, which on the server is linked to the folder test.

This is my config (httpd-vhosts.conf):

<VirtualHost server.com>
DocumentRoot "c:/www/"
ServerName server.com
Alias /new "c:/www/test"
<Directory "c:/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>


So, to be clear, if someone browses to:


  • server.com/test (the server uses folder test, but server.com/new is shown in the browser.

  • server.com/ (the same as above => server.com/new is shown in the browser)

  • server.com/index.php (the server uses index.php from folder test, browser shows server.com/new/index.php

  • server.com/test/index.php (the same as above)

  • server.com/test/folder2/index.php?variable=1 (the server uses index.php from folder test/folder2 and gives the variables to the php file. browser shows server.com/new/folder2/index.php?variable=1)



I have tried different .htaccess rewrites, but I can't get it working.

Maybe I don't have to use the alias?

Don't just give me a link to the apache manual, because I've read it, but can't get it to work...

Answer

You don't need Alias for this so comment out:

Alias /new "c:/www/test"

Then have these rules:

<VirtualHost server.com>
    DocumentRoot "c:/www/"
    ServerName server.com

    <Directory  "c:/www/">
        Options +Indexes +Includes +FollowSymLinks +MultiViews
        AllowOverride All
        Require all granted
    </Directory>

    DirectoryIndex index.php
    RewriteEngine On

    RewriteRule ^/?$ /new/ [L,R=301]

    RewriteCond %{THE_REQUEST} \s/+test(\S*)\s [NC]
    RewriteRule ^ /new%1 [R=301,NE,L]

    RewriteRule ^/?new(/.*)?$ /test$1 [L,NC]

</VirtualHost> 
Comments