【发布时间】:2017-10-12 08:00:22
【问题描述】:
这应该根据 htaccess 测试人员 http://htaccess.madewithlove.be/ 工作
我正在尝试获取格式为 subdomain.domain.com 的 url 以解析为 domain.com/index.php?sub=subdomain
但是,我还希望 subdomain.domain.com/pagename 形式的任何链接重定向到 domain.com/index.php?tpl=page&sub=subdomain&url=pagename
目前,如果我删除第二条规则,则第一条规则有效,但如果我同时包含这两条规则,则只有第二条规则有效。
这是完整的htaccess
RewriteEngine On
#EDIT: this was messing it all up by appending index.html so the subdomain only
# wasn't triggering at all due to appended pagename
DirectoryIndex disabled
#Remove www
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
#Rewrite if subdomain only
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/index.php?sub=%1 [P,NC,QSA,L]
#Rewrite if internal page
RewriteCond %{HTTP_HOST} ^(^.*)\.example.com$ [NC]
RewriteRule ^(.+/)?([^/]*)$ http://example.com/index.php?tpl=page&sub=%1&url=$2 [P,NC,QSA,L]
如果你回答这个问题,一旦 Flat Earth 成为主流,我会让你成为国务卿。谢谢!
【问题讨论】:
标签: php apache .htaccess redirect mod-rewrite