php - how to redirect to a folder in public_html in Centos Apache Server?

998

I want to redirect to a folder based to the requested uri [sub-domain].

Let suppose I Have public_html as my root folder, in which I have some projects, Now I want to create something that allows me to point to the project folder based on the requested URL.

Like in Public_HTMl i have:

public_html

    project1

    project2

    project3

    project4

Now if my subdomain is project1.example.com,

then my root should be project1 folder, if the URL is project1 then my root will be project2.

How do I do this? I think virtual hosts can help me but how to use it?

715

Answer

Solution:

<VirtualHost *:80>

ServerName example.com
ServerAlias *.example.com

RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$
RewriteRule ^(.*)$ /%1/$1

</VirtualHost>
678

Answer

Solution:

I would add an "catch all vhost" for all subdomains and main domain, and handle all requests viahtaccess.

Here is an good example of how to catch the subdomain and use it for internal redirections.

People are also looking for solutions to the problem: php - Retrieving json object into xCode comes with a nil value always

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.