复杂.htaccess重定向

时间:2015-12-18 11:16:12

标签: apache .htaccess mod-rewrite redirect

“domain.com”是我的根域,我还有其他几个域作为别名域链接到它 - 让我们称之为“domain2.com”。它们与root完全相同,但用于替代拼写和.net,.de等扩展。

我还为朋友重新定位到他们自己的网站空间时购买了一些域名 - 让我们称之为“domain3.com”。

最后我有一个域名“domain4.com”,它也是一个别名,但应该有不同的行为。

我在这里列出了我的要求:

domain.com
-> root domain
-> wildcard SSL certificate
-> remove www
-> redirect anything to https://
-> "foo" subdomain is automatically redirected to /.sub-foo/ folder
   (server setting that I have no influence in)
-> if possible preserve https://foo.domain.com/ in the address bar instead 
   of https://domain.com/.sub-foo/

domain2.com
-> alias domain linked to domain.com
-> no SSL certificate
-> redirect everything to domain.com, including Request URI and any subdomains
-> address bar should change to domain.com

domain3.com
-> alias domain linked to domain.com
-> redirect everything to external.domain.com, including Request URI
   NOT including any subdomains
-> address bar should change to external.domain.com

domain4.com:
-> alias domain linked to domain.com
-> no SSL certificate
-> remove www
-> remove ANY subdomain used
-> remove ANY request URI used
-> ALWAYS redirect to the domain, no matter what
-> redirect that domain to the folder domain.com/subdir/

编辑:好的,经过大量的尝试和错误,我终于找到了一个基本上完整的解决方案。

### domain3.com ###
RewriteCond %{HTTP_HOST} ^(.*\.)?domain3\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(\.sub-.*\/)?(.*)?$ [NC]
RewriteRule ^(.*)$ http://external.domain.com/%3 [L,R]

### domain4.com ###
# check if any subdomain is used and remove it
RewriteCond %{HTTP_HOST} !^domain4\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)?domain4\.com$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]

# check if redirected subdomain is used and remove it
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(\.sub-.*\/)(.*)?$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]

# check if any request is used and remove it
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteCond %{REQUEST_URI} ^(\/)(.*)$ [NC]
RewriteRule ^(.*)$ http://domain4.com/ [L,R]

# redirect to subdir/ directory
RewriteCond %{HTTP_HOST} ^domain4\.com$ [NC]
RewriteRule !^subdir/ subdir/ [L,NC]

### domain.com ###
# remove www subdomain and redirect to HTTPS
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]

# redirect subdomain to https
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(.*\.)domain\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]

# redirect http to https
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]

### domain2.com ###
# remove www subdomain and redirect to HTTPS root domain
RewriteCond %{HTTP_HOST} !^domain2\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]

# redirect subdomain to https root domain
RewriteCond %{HTTP_HOST} !^domain2\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*\.)domain2\.com$ [NC]
RewriteRule ^(.*)$ https://domain.com/$1 [L,R]

### copy domain2.com code for every other "domain2.com" like domain
### I have this code 6 times in my htaccess

对于那些想知道domain2.com的第二个RewriteRule部分的人 - 我不能使用“https://%1domain.com/ $ 1”这是“正确的”方法,因为我的服务器将子域改为“/。 sub-foo /“在处理.htaccess之前。

唯一不起作用的东西(我不确定我是否会让它继续工作)是地址栏显示https://domain.com/.sub-foo/bar.php而不是https://foo.domain.com/bar.php 如果他可以停用自动重定向功能,我会尝试与我的托管服务商联系,以便我可以通过.htaccess单独解决这个问题。

如果有人知道如何缩短这个怪物 - 请帮助。

0 个答案:

没有答案