htaccess - 允许多个域来源,强制https,强制www,用子目录重写url

时间:2016-06-02 21:02:51

标签: .htaccess redirect https url-rewriting subdirectory

我想创建一个.htaccess

  1. 允许来源(https://www.example.comhttps://example.com以及https://subdomain.example.com
  2. 强制https
  3. 强制www
  4. 使用子目录
  5. 重写url

    编辑:这是我的新.htaccess文件

        RewriteEngine on
    
        Header add Access-Control-Allow-Origin "https://www.example.com, https://example.com, https://subdomain.example.com"
        Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
        Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type, X-CSRF-Token"
    
        RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
        RewriteCond %{HTTPS} off
        RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
        RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]
    
        RewriteRule ^(path/to/directory/file)/([^/]+)/?$ $1.php?u=$2 [NC,L,QSA]
    

    这是我的.htaccess文件

    RewriteEngine on
    
    Header add Access-Control-Allow-Origin "https://www.example.com, https://example.com, https://subdomain.example.com"
    Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
    Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type, X-CSRF-Token"
    
    RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.example\.com$
    
    RewriteCond %{SERVER_PORT} 80 
    RewriteRule ^(.*)$ "https\:\/\/www\.example\.com\/$1" [R=301]
    
    
    RewriteRule ^path/to/directory/file/([^/]*)$ path/to/directory/file.php?u=$1 [L]
    

    修改

    在^之后删除第一个斜杠,使其按照in this subject的说法运行。

2 个答案:

答案 0 :(得分:2)

用以下内容替换所有现有代码:

RewriteEngine on

Header add Access-Control-Allow-Origin "https://www.example.com, https://example.com"
Header add Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
Header add Access-Control-Allow-Headers "origin, x-requested-with, content-type, X-CSRF-Token"

RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,R=301,NE]

RewriteRule ^(path/to/directory/file)/([^/]+)/?$ $1.php?u=$2 [NC,L,QSA]

确保在测试之前清除浏览器缓存。

答案 1 :(得分:1)

允许来源:

    MyConnection.EnableExtensions(True)
    MyConnection.LoadExtension("System.Data.SQLite.dll", "sqlite3_fts5_init") ''/// Or "SQLite.Interop.dll" as you need.

强制HTTPS:

<IfModule mod_headers.c>
    Header set Access-Control-Allow-Origin "*"
</IfModule>

强制WWW:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]

子域名的网址:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com[nc]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,nc]