试图覆盖htaccess文件

时间:2018-02-19 13:32:28

标签: .htaccess url-redirection http-redirect

我正在尝试编写一个涵盖我需要的所有基础的htaccess文件,但我有很多非常具体的行动要覆盖。

我有三个域:.com .co.uk和.london,我希望所有流量最终都在https://www.somepage.com,除了任何到.London的流量,必须重定向到特定的mysite / London。 php页面

所以我的所有条件都是:

force www prefix
force https
remove extensions
force .co.uk to .com domain
force any .london traffic to specific /london page

到目前为止,我有:

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]

所以我需要添加最后两个条件

force .co.uk to .com domain
force any .london traffic to specific /london page

但我很难让所有事情一起工作。

编辑:

仍然努力让所有条件一起工作,所以为了清楚起见,在这里添加一些例子。

www.example.com - > https://www.example.com

http://example.com - > https://www.example.com

http://www.example.com/mypage.php - > https://www.example.com/mypage

www.example.co.uk - > https://www.example.com

http://www.example.co.uk - > https://www.example.com

http://www.example.co.uk/mypage.php - > https://www.example.com/mypage

www.example.london - > https://www.example.com/london

http://example.london - > https://www.example.com/london

http://example.london/anything - > https://www.example.com/london

所以所有URL都必须添加'www',.com域名,https强制,php扩展名已删除。

我已尝试了数百个代码片段和组合,但无法同时使所有条件都能正常工作。

1 个答案:

答案 0 :(得分:1)

您可以使用以下规则:

# force .co.uk to .com domain
RewriteCond %{HTTP_HOST} ^(www\.)?\.example\.co\.uk$
RewriteRule ^ https://www.example.com%{REQUEST_URI} [NE,L,R=301]

# force any .london traffic to specific /london page
RewriteCond %{HTTP_HOST} ^(www\.)?example\.london$
# this line is important to avoid Redirect loop error
RewriteCond %{REQUEST_URI} !/london/page
RewriteRule ^ /london/page%{REQUEST_URI} [L,R]

# Force WWW prefix
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Force SSL
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]

# Remove .php extension
RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php
RewriteRule (.*)\.php$ /$1/ [L,R=301]