我正在开发一项服务,我需要为动态创建的页面提供不同的域名,例如,我会有类似的内容:
example.net/?client=hello
我认为,通过htaccess,我可以做到这一点:
example.net/hello
是否可以将域指向该“文件夹”?
答案 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]