2010-08-21 13 views
6

Cualquier solicitud de www.example.com/* debe ser redirigido a www.example.com/blog/*.htaccess: redirigir todas las peticiones a un subdirectorio, excepto si existe un directorio exacta

Si hay www. prefijo, agregarlo.

Es importante saber que, si existe algún directorio que coincida con el URI de solicitud, no lo redirija.

Ejemplo:

(www.)example.com/<request> ->www.example.com/blog/<request> excepto <request> === <dirname>

Después de las 3 condiciones anteriores, ¿cómo puedo código A .htaccess? ¡Por favor ayuda! Thx ;-)

Respuesta

12

Esto debería hacer lo que usted quisiera. También agregué un "no redirigir si este archivo existe", ya que no estaba seguro de lo que estaba en sus directorios existentes. Puedes intentar eliminarlo sacando el segundo RewriteCond si no lo deseas, pero creo que es probablemente necesario hasta cierto punto.

RewriteEngine On 

# Check if the requested path is not a real file or a 
# real directory 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
# If the current request doesn't start with "blog", and 
# it's not a real file or directory based on the above 
# conditions, add "blog" to the front of the request, and 
# mark an environment variable that indicates we'll need 
# to redirect 
RewriteRule !^blog blog%{REQUEST_URI} [E=CHANGED:TRUE] 

# Check if the host doesn't start with "www.", or if we've 
# marked the change variable above, since in either of those 
# cases we need to perform a redirection (We do it this way, 
# since we'll at most send one redirect back to the client, 
# instead of the potential two we might send if we didn't 
# combine the checks) 
RewriteCond %{HTTP_HOST} !^www\. [OR] 
RewriteCond %{ENV:CHANGED} =TRUE 
# Capture the non-"www." part of the host, regardless of 
# whether or not the "www." is there 
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ 
# Redirect anything to the corrected URL, using the 
# backreference from the above condition, and the entirety of 
# the requested path (possibly modified by the above RewriteRule) 
RewriteRule ^.*$ http://www.%2/$0 [R=301,L] 
+0

Está funcionando bien ... Gracias. Oye, probablemente si puedes explicar brevemente cada línea añadiéndole comentarios, sería mejor. Gracias de todos modos. –

+1

@Digit Inrok: ese es un buen punto, he agregado comentarios ahora que espero que lo expliquen todo. –

+0

+1 por los comentarios – WarFox

Cuestiones relacionadas