0

I am trying to redirect my project folders one level up in stucture. I would like to change URL adresses as follows:

to

In root folder I do not have these project folders, they are in "projects" folder

the structure is like this

/root/
    /projects/
        /project1/
            index.php
        /project2/
            index.php
        /project3/
            index.php
index.php
.htaccess

So if user enters URL http://localhost/my-website/project1/, the URL stays the same but it works as if he entered the full adress. Is this achievable? I tried so many options so far and I am still not able to do it...

1 Answers1

1

First of all, don't use a .htaccess file, if you can modify the Apache config files. It's better to add these types of configuration inside the vhost config instead.

With that said:

.htaccess

<Location "/">

    AllowOverride None
    Options FollowSymLinks
    Require all granted

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        # RewriteRule ^project(.*) projects/project$1 [QSA,L]
        # RewriteRule ^project([0-9*]?)/(.*) projects/project$1/$2 [QSA,L]
    </IfModule>

</Location>

/my-website/project1/ becomes /my-website/projects/project1/

/my-website/project1/whatever becomes /my-website/projects/project1/whatever

Uncomment the one that is more up your ally, the first one is more generic and catches all after project, the second one is more specific and looks for a number.

Anuga
  • 2,619
  • 1
  • 18
  • 27
  • Thank you for answer. This works for me. I am just trying to edit the regex for "project" because they are actually not named as "project1, project2..." and I am struggling. Anyway thank you – Eduard Farkaš Feb 06 '19 at 11:11
  • One last question, I ended up with "RewriteRule ^(.*) projects/$1 [QSA,L]" which works just fine, I am just curious if there is a way of editing it in such a way that it doesn't get redirected back to "projects" folder when I enter the URL without "/". – Eduard Farkaš Feb 06 '19 at 11:34
  • Then you probably need two rules. One for the root, and one for the sub-uris. https://httpd.apache.org/docs/current/mod/mod_rewrite.html here's the documentation. There are some good examples as well. – Anuga Feb 06 '19 at 15:04