如何在htaccess中处理多个场景,包括blog.domain.com到domain.com/blog,强制www,强制ssl和url重定向?

时间:2014-07-23 19:36:03

标签: regex apache .htaccess mod-rewrite redirect

我正在尝试使用主机上的htaccess文件做一些事情。

  1. 强制WWW

  2. 强制使用SSL

  3. 将blog.domain.com重定向到domain.com/blog(博客以前是tumblr)

  4. 将blog.domain.com上的各个旧帖子重定向到domain.com/blog

  5. 目前我满意1-3,但我无法弄清楚如何满足4.我似乎无法测试或验证这一点。我已尝试使用http://htaccess.madewithlove.be/,但我无法正确验证它。我也觉得我可以在htaccess中更有效地做这些事情。任何满足4的帮助都会非常感激,或者做得更聪明会非常感激。

    这是我目前的.htaccess:

    Options +FollowSymLinks
    RewriteEngine on
    
    # Force WWW without forcing certain incoming subdomains to have a WWW (this should handle forcing SSL as well, but I've found I need the other Force SSL rule to make sure that it happens)
    RewriteCond %{HTTP_HOST} !^$
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{HTTP_HOST} !^blog\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    
    # Send requests to blog.domain.com to domain.com/blog
    RewriteCond %{HTTP_HOST} ^blog\. [NC]
    RewriteCond %{HTTPS}s ^on(s)|
    RewriteRule ^ http%1://%{HTTP_HOST}/blog [R=301,L]
    
    # Force SSL
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://www.domain.com/$1 [R,L]
    
    ErrorDocument 404 https://www.domain.com/404
    
    # For certain query parameters, redirect to support page
    RewriteCond %{REQUEST_URI}  ^(.*)$
    RewriteCond %{QUERY_STRING} ^v=([0-9]*)$
    RewriteRule ^(.*)$ https://www.domain.com/support? [R=301,L]
    
    # Tumblr to Wordpress Redirects
    Redirect 301 /post/91531634276/blog-title https://www.domain.com/blog/blog-title/
    Redirect 301 /post/45441292642/blog-title-2 https://www.domain.com/blog/blog-title-2/
    Redirect 301 /post/46492436919/blog-title-3 https://www.domain.com/blog/blog-title-3/
    

1 个答案:

答案 0 :(得分:1)

Redirectmod_alias指令,不应与mod_rewrite混合使用。将这些规则放在RewriteEngine On行:

下方
RewriteCond %{HTTP_HOST} ^blog\. [NC]
RewriteRule ^post/\d+/([^/]+)/?$ https://www.domain.com/blog/$1/ [L,NC,R=301]