【发布时间】:2016-10-02 19:01:59
【问题描述】:
我想创建一个 .htaccess 到
- 允许来源(https://www.example.com 和 https://example.com 和 https://subdomain.example.com)
- 强制 https
- 强制 www
- 用子目录重写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]
-
这条规则有效:(http://example.com/file/xxxx)
RewriteRule ^file/([^/]*)$ path/to/directory/file.php?u=$1 [L] -
编辑:此规则有效 (http://example.com/path/to/directory/file/xxxx)
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