使用https,非www和子文件夹

时间:2018-04-04 23:08:29

标签: .htaccess

我需要重定向以下内容:

  1. 将所有请求重定向到SSL
  2. 将www重定向到非www
  3. 将根域的请求重定向到/子目录/但是如果存在现有目录则不重定向。 (仍然保持规则1和规则2)
  4. 将旧网址example.com/REQUEST重定向到example.com/sub-directory/REQUEST
  5. 我已经能够做到这一切,唯一的问题是我永远无法熟悉mod_redirect规则因此无法使其高效。在这里经历了几乎所有相关的问题,但仍然很少混淆,并且当然希望删除冗余......

    所以这是我在.httaccess

    中的代码
    # Remove www + force https on everything
    RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L,NE]
    
    
    #Add Sub-directory for fixing old urls when site  was in root 
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteCond %{REQUEST_URI} !^/sub-directory/
    #Only apply rules for above condition if requested file or directory doesn't exist
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /sub-directory/$1 [NC,R=301]
    
    # If reqeust is made to only domain root then redirect to domain.com/sub-directory/
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteRule ^(/)?$ /sub-directory/ [L,R=301]
    

    现在上面的内容可以重定向所有1,2,3,4条件,如上面开头的

    但是有多个请求。 例如:对于条件3,它首先重定向到http://example.com - > https://example.com ---> http://example.com/sub-directory/

    我知道,并不是什么大不了的事,可以忍受它,但是,它仍然可以像是:

    # Remove www + force https on everything
    RewriteCond %{HTTP_HOST} ^www\. [OR,NC]
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
    RewriteRule ^ https://example.com/sub-directory%{REQUEST_URI} [R=301,L,NE]
    

    但是上面也将所有内容重定向到子目录,因此没用。任何建议,如何使其高效和重点,即。仅在1个重定向内部实现所有条件?

0 个答案:

没有答案
相关问题