是否可以将域分配给动态创建的“文件夹”?

时间:2013-11-17 00:41:11

标签: php .htaccess dns directory

我正在开发一项服务,我需要为动态创建的页面提供不同的域名,例如,我会有类似的内容:

example.net/?client=hello

我认为,通过htaccess,我可以做到这一点:

example.net/hello

是否可以将域指向该“文件夹”?

1 个答案:

答案 0 :(得分:2)

我不确定point a domain to that folder你的意思。但是,如果您想将example.net/hello重写为example.net/index.php?client=hello,您可以这样做:

# check so it's not a file
RewriteCond %{REQUEST_FILENAME} !-f
# check so it's not a directory
RewriteCond %{REQUEST_FILENAME} !=d
# do we have a client name in the url?
# valid names are not case sensitive and containing letters between a-z
Rewritecond %{REQUEST_URI} ^([a-zA-Z]+)$
# pass the client name to index.php 
RewriteRule ^(.*)$ index.php?client=$1 [L,QSA]

反过来说,如果您想将index.php?client=hello重写为名为hello的现有文件夹,这应该有效:

# check so it's not a directory
RewriteCond %{REQUEST_FILENAME} !=d
# check so we have a valid query string
RewriteCond %{QUERY_STRING} ^client=([a-zA-Z]+)$
# check so the client directory really exists
RewriteCond %1 -d
# rewrite to client folder and strip of the query string 
RewriteRule ^index\.php$ %1? [L]