2013-08-18 16 views
6

risultato desiderato:.htaccess riscrivere: sottodominio come GET var e percorso come GET var

http://example.com/     -> index.php 
http://www.example.com/    -> index.php 
http://hello.example.com/   -> index.php?subdomain=hello 
http://whatever.example.com/  -> index.php?subdomain=whatever 
http://example.com/world   -> index.php?path=world 
http://example.com/world/test  -> index.php?path=world/test 
http://hello.example.com/world/test -> index.php?subdomain=hello&path=world/test 

Con la .htaccess ho in questo momento, posso raggiungere uno o l'altro re-mapping, ma non entrambe lo stesso tempo.

RewriteEngine On 

# Parse the subdomain as a variable we can access in PHP, and 
# run the main index.php script 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTP_HOST}  !^www 
RewriteCond %{HTTP_HOST}   ^([^\.]+)\.([^\.]+)\.([^\.]+)$ 
RewriteRule ^.*$ index.php?subdomain=%1 

# Map all requests to the 'path' get variable in index.php 
RewriteBase/
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?path=$1 [L] 

Ho difficoltà a combinare i due ... eventuali suggerimenti, per favore?

EDIT
Il comportamento indesiderato che sto vivendo ora è che se ho un sottodominio e un percorso dopo il .com /, solo il sottodominio sarà passato attraverso, vale a dire:

http://hello.example.com/world-> index.php?subdomain=hello 
+0

http://stackoverflow.com/questions/25582265/mod-rewrite-setting-subdomain-and-directory-to-get-variable – shaunw

+0

forse è meglio se si combinano VirtualHost per la gestione dei sottodomini e .htaccess per la gestione dell'URL riscrivere. Due anni dopo, come risolvi questo? –

risposta

7

Usa la prima regola per aggiungere il parametro subdomain, senza cambiare l'URI, quindi utilizzare il 2 ° regola per instradare l'URI per index.php:

RewriteEngine On 

# Parse the subdomain as a variable we can access in PHP, and 
# run the main index.php script 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTP_HOST}  !^www 
RewriteCond %{HTTP_HOST}   ^([^\.]+)\.([^\.]+)\.([^\.]+)$ 
RewriteRule ^(.*)$ /$1?subdomain=%1 

# Map all requests to the 'path' get variable in index.php 
RewriteRule ^index\.php$ - [L] 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)$ /index.php?path=$1 [L,QSA] 

la seconda regola ha bisogno del per avere il flag QSA, altrimenti la stringa di query della prima regola viene persa.

+0

Ah, "query string append", interessante! Molto informativo, grazie. –

+0

Ciao Jon Lin come riscrivere come http://admin.example.com/ -> http://example.com/admin/ – SKG

+0

Il codice sopra non sembra funzionare per l'ultimo esempio dell'OP (http: // hello.example.com/world/test). Ripeterà il secondo segmento (/ test) due volte in 'path =' var come '/ world/test/test'. – shaunw

Problemi correlati