Pregunta

Estoy tratando de crear un sitio con CodeIgniter. Tendré varios dominios servidos desde este servidor, y lo que estoy tratando de hacer es separar las solicitudes HTTP para www.example1.com de las de www.example2.com y luego redirigirlas a sus carpetas de aplicaciones correctas.

Digamos que esta es mi estructura de directorio:

  • sistema
    • aplicación
      • ejemplo1
      • ejemplo2

Entonces esta solicitud

www.example1.com/gallery/

se redirigiría a la carpeta exmaple1.

¿Alguien tiene algún ejemplo de código para hacer esto? Obviamente, necesitaría usar el módulo ReWrite ...

Miré alrededor de la documentación de Apache pero no pude llegar a ninguna parte. Avíseme si necesita más información sobre esto.

¿Fue útil?

Solución

El uso de VirtualHost (y Alias, Rewrite) son las respuestas principales, pero si cree que agregará / eliminará otros hosts con la misma configuración, entonces consideraría el módulo mod_macro . Este es un módulo de terceros que lo ayudará a simplificar la configuración de apache y evitar errores de copiar / pegar.

Un ejemplo simple con su diseño, defina lo siguiente:

<Macro vh80 name>
 <VirtualHost *:80>
   DocumentRoot /system/application/$name
   ServerName www.$name.com
   CustomLog /system/application/$name/logs/access.log virtcommon
   ErrorLog /system/application/$name/logs/error.log
 </VirtualHost>
</Macro>

Y luego, cuando habilite un sitio, use la siguiente directiva:

  

Use vh80 example1

Que configurará www.example1.com para usar el directorio configurado como raíz así como el directorio de registro configurado.

Otros consejos

Lo que necesita se llama VirtualHost para hacer www.example1 .com y www.exaplme2.com apuntan a una carpeta diferente en su sistema de archivos.

Si además desea tener una ruta diferente en el URI para servir el contenido principal, tiene varias alternativas:

  1. Cree físicamente la carpeta y no realice más cambios

  2. Cree físicamente un enlace (galería denominada que apunta a la carpeta raíz principal de virtualhost) a la carpeta y use opción FollowSymLinks para VirtualHost

  3. Use una Alias ?? en el VirtualHost

    Alias /gallery /
    
  4. Usar mod_rewrite

    RewriteRule /gallery/(.*) /$1 [QSA]
    

La más simple (si CodeIgniter lo permite) sería la opción 1 o 2.

Fragmento de la documentación de VirtualHost:

Running several name-based web sites on a single IP address.

Your server has a single IP address, and multiple aliases (CNAMES) 
point to this machine in DNS. You want to run a web server for 
www.example.com and www.example.org on this machine.

Note

Creating virtual host configurations on your Apache server does 
not magically cause DNS entries to be created for those host 
names. You must have the names in DNS, resolving to your IP 
address, or nobody else will be able to see your web site. 
You can put entries in your hosts file for local testing, 
but that will work only from the machine with those hosts 
entries.

Server configuration

# Ensure that Apache listens on port 80
Listen 80

# Listen for virtual host requests on all IP addresses
NameVirtualHost *:80

<VirtualHost *:80>
DocumentRoot /www/example1
ServerName www.example.com

# Other directives here

</VirtualHost>

<VirtualHost *:80>
DocumentRoot /www/example2
ServerName www.example.org

# Other directives here

</VirtualHost>

The asterisks match all addresses, so the main server serves no requests. 
Due to the fact that www.example.com is first in the configuration file, 
it has the highest priority and can be seen as the default or primary 
server. That means that if a request is received that does not match 
one of the specified ServerName directives, it will be served by this
first VirtualHost.

Estas son realmente dos preguntas:

  1. ¿Cómo diferenciar www.example1.com y www.example2.com? La respuesta a esto es la VirtualHost .

  2. ¿Cómo hacer que / gallery apunte al ejemplo1? Esto se hace con la directiva Alias.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top