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

htaccess - allow multiple domain origins, force https, force www, rewrite url with subdirectory

我想为

创建一个 .htaccess
  1. 允许来源 (https://www.example.com and https://example.com and https://subdomain.example.com)
  2. 强制 https
  3. 强制 www
  4. 用子目录重写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)/([^/]+)/?$ .php?u= [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\/" [R=301]


RewriteRule ^path/to/directory/file/([^/]*)$ path/to/directory/file.php?u= [L]

编辑:

考虑删除 ^ 后的第一个斜杠,使其按所说工作 in this subject

允许来源:

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

强制 HTTPS:

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

强制 WWW:

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

URL 到子域:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example [L]

将您现有的所有代码替换为:

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)/([^/]+)/?$ .php?u= [NC,L,QSA]

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