【问题标题】:htaccess - allow multiple domain origins, force https, force www, rewrite url with subdirectoryhtaccess - 允许多个域来源,强制 https,强制 www,用子目录重写 url
【发布时间】:2016-10-02 19:01:59
【问题描述】:

我想创建一个 .htaccess 到

  1. 允许来源(https://www.example.comhttps://example.comhttps://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)/([^/]+)/?$ $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 所说的那样工作。

【问题讨论】:

  • 我看到的唯一区别是第一条规则以 without 斜线开头,第二条规则以 with 斜线开头。
  • 我进行了编辑。它并没有改变什么(我测试了第二个有和没有它的工作)。为了更清楚,我删除了第二个。
  • @anubhava 对不起,我不明白你的问题。 xxxx 表示 u 变量的值。这是一个用户的 id。
  • 您的系统中是否还有其他 .htaccess 或任何其他规则?
  • 对。谢谢! path/to/directory 上有一个错误的 .htaccess,剩余的行不正确。我删除了它,子目录问题就解决了。我测试了其他规则仍然可以继续:https://example.com/

标签: .htaccess redirect https url-rewriting subdirectory


【解决方案1】:

用这个替换所有现有的代码:

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]

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

【讨论】:

  • 谢谢大家工作正常:允许 2 个来源,强制 https,强制 www,使用子目录重定向。 :)
  • 最后一个问题,如果您愿意,如果我想添加像 RewriteRule ^(path/to/directory/file)/([^/]+)/?$ $1.php?u=$2 [NC,L,QSA] 这样的连续重写规则以便拥有几个友好的 url,我应该保留相同的标志吗?
  • 嗨,从子域加载 fontawesome 图标会产生此错误,请问我该如何解决? Font from origin 'https://example.com' has been blocked from loading by Cross-Origin Resource Sharing policy: The 'Access-Control-Allow-Origin' header contains multiple values 'https://www.example.com, https://example.com', https://subdomain.example.com' but only one is allowed. Origin 'https://subdomain.example.com' is therefore not allowed access.
  • 是的,子域目录也位于 /home/sitename 目录中,而 htaccess 位于 /home/sitename/public_html 目录中,以防止其进行 www 重定向。但是我仍然对子域上的 Access-Control-Allow-Origin 有问题。
  • 我想我应该有一个 .htaccess 作为子域文件的目录。没有 www 和 https 强制。 - 编辑,通过 cPanel 重定向完成。但是我仍然坚持使用允许来源标头,就像它不起作用
【解决方案2】:

允许来源:

<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]

强制万维网:

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

子域的 URL:

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

【讨论】:

  • 谢谢 www 和 https 重定向不再有问题。是否可以在允许的来源中仅添加 https://www.example.comhttps://www.example.com?而且我不太了解子域部分的 URL。
猜你喜欢
  • 2014-02-15
  • 2014-01-20
  • 2011-06-17
  • 1970-01-01
  • 2016-07-05
  • 2015-09-11
  • 2016-04-05
  • 2016-11-12
  • 2015-07-18
相关资源
最近更新 更多