开发者

.htaccess redirect from subdomain not passing subdomain part

I have an .htaccess file which catches all subdomains (I am using a wildcard DNS record) and redirects them to a secure login page, but only if the subdomain isn't www. My code for this is:

# Turn rewriting on
RewriteEngine On

# If no subdomain is supplied then add www by default
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]

# If the subdomain isn't www then redirect to the login page
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://secure.mydomain.com/a/%1 [R=301,L]

This is working in part. The catching of the www and inserting if it's not there is fine, but what it's not doing is if I type:

http://sample.mydomain.com

I want:

https://secure.mydomain.com/a/sample

but the subdomain bit isn't being appended to the end. I thought the %1 bit would do this but it seems to not be working.

Also, as a second thought, how can I catch:

https://secure.mydomain.com

where nothing is after .com and redirect it back to:

http://www.mydomain.com

Anybody any开发者_如何学Python thoughts?


Do something like that:

# If the subdomain isn't www then redirect to the login page
RewriteCond "%{HTTP_HOST}" !^www.* [NC]
RewriteCond "%{HTTP_HOST}" ^([^\.]+).*$
RewriteRule ^(.*)$ https://secure.mydomain.com/a/%1 [R=301,L]

In general you have to catch the subdomain part and backreference it. So I added a second RewriteCond that does just that. Backreferences in RewriteConds are accessed with '%n' patterns in RewriteRules.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜