2010-08-21 12 views
6

Qualsiasi richiesta di www.example.com/* deve essere reindirizzato a www.example.com/blog/*.htaccess: reindirizzare tutte le richieste per una sottodirectory a meno che una directory esatto esiste

Se non www. prefisso, inserirlo.

Importante: se esiste una directory corrispondente all'URI della richiesta, non reindirizzare.

Esempio:

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

seguito di quanto sopra 3 condizioni, come faccio a codificare un .htaccess? Per favore aiuto! Thx ;-)

risposta

12

Questo dovrebbe fare quello che volevi. Ho anche aggiunto in un "non reindirizzare se questo file esiste", dal momento che non ero sicuro di ciò che era nelle directory esistenti. Puoi provare a rimuoverlo tirando fuori il secondo RewriteCond se non lo vuoi, ma penso che sia probabilmente necessario in una certa misura.

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

Sta funzionando bene ... Grazie. Ehi, probabilmente se puoi spiegare brevemente ogni riga aggiungendo commenti ad esso, sarebbe meglio. Grazie ancora comunque. –

+1

@Drigit Inrok - Questo è un buon punto, ho aggiunto dei commenti ora che spero possano spiegare tutto. –

+0

+1 per i commenti – WarFox

Problemi correlati