2013-08-18 21 views
6

İstenilen sonuç: Şu anda var .htaccess, bir tane elde edebilirsiniz ya da diğer yeniden haritalama ileyeniden .htaccess: GET var olarak GET var ve yol olarak alt alan

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 

, ancak ikisini de Aynı zaman.

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] 

İkisini birleştirerek zor bir zaman geçiriyorum ... herhangi bir işaretçi, lütfen?

DÜZENLEME
şimdi yaşıyorum istenmeyen davranış bir alt alan adı ve .com/sonra bir yol varsa, sadece alt alan aracılığıyla yani geçilecek olmasıdır:

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

yeniden yazmak. İki yıl sonra, bunu nasıl çözersiniz? –

cevap

7

Kullanımı ilk kural index.php için URI sonra, URI değiştirmeden subdomain parametre eklemek rotaya 2 kuralını kullanmak:

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] 

saniye kuralı , QSA bayrağına sahip olmak için gerekiyorsa, aksi halde ilk kuralın sorgu dizesi kaybolur. Alt satırları işlemek için VirtualHost'ları ve URL'leri işlemek için .htaccess'i birleştirirseniz, belki de daha iyisi

+0

Ah, 'sorgu dizisi ekle', ilginç! Çok bilgilendirici, teşekkürler. –

+0

Merhaba Jon Lin http://admin.example.com/ -> http://example.com/admin/ – SKG

+0

gibi nasıl yeniden yazılacağı Yukarıdaki kod, OP'nin son örneğinde (http: // hello.example.com/world/test). İkinci segmenti (/ test) iki kez 'path =' var '/ world/test/test' olarak tekrar edecektir. – shaunw