https in htaccess and order of rules (using Expression Engine)
我正在表达式引擎中构建一个站点,其中一部分需要是 https。该站点还使用了新域名(新域名 www.example-new.com 旧域名 www.example.old.com)。
我想做以下事情:
到目前为止,我有以下代码可以满足前两个要求:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <IfModule mod_rewrite.c> RewriteEngine On # Force www RewriteCond %{HTTP_HOST} ^example-new.com$ [NC] RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L] # Removes index.php RewriteCond $1 !\\.(gif|jpe?g|png)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] </IfModule> AddType x-httpd-php53 .php |
我似乎在绕圈子,所以我有两个问题可以帮助我编写其他重写(尽管随时分享建议...):
1) 需求 3 和 4 的代码是否应该位于"删除 index.php"代码之前?
2) 该职位是否对来自旧网站的重定向有任何影响,例如www.example-old.com/some-link-here.asp 将被重定向到 www.example-new.com/some-new-link-here
谢谢,
格雷戈
1) 从 ExpressionEngine URL 中删除 \\'index.php\\'
1 2 3 4 | RewriteCond $1 !\\.(gif|jpe?g|png)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] |
2) 将 \\'www\\' 添加到所有 URI
1 2 | RewriteCond %{HTTP_HOST} !^www\\.(.*)$ [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] |
3) 对任何以 /extranet
开头的 URI 强制使用 https://
1 2 3 | RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC] RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] |
4) 重定向不是 /extranet
的 https:// URI
1 2 3 | RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC] RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] |
把它们放在一起,这是你的完整的 RewriteRules 集:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} !^www\\.(.*)$ [NC] RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC] RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] RewriteCond %{HTTPS} on RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC] RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L] RewriteCond $1 !\\.(gif|jpe?g|png)$ [NC] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L] </IfModule> |
如果有帮助...我昨天刚刚为电子商务网站做了这个...这是我的 .htaccess 文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | # Remove WWW from URL RewriteCond %{HTTP_HOST} ^www\\.(.*)$ [NC] RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L] # Add a trailing slash to paths without an extension RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !(\\.[a-zA-Z0-9]{1,5}|/)$ RewriteCond %{REQUEST_METHOD} !=POST RewriteRule ^(.*)$ $1/ [L,R=301] #remove index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php/$1 #Force HTTPS on checkout/account pages RewriteCond %{HTTPS} off RewriteCond %{REQUEST_URI} (checkout|account) RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] #remove HTTPS on all other pages RewriteCond %{HTTPS} on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(img|_|images|checkout|account) RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI} [R=301,L] |
1 2 3 4 | # force www on hostname, but keep same protocol (http/https) RewriteCond %{HTTP_HOST} !^www\\.(.+) RewriteCond %{HTTPS}s ^(on(s)|offs) RewriteRule ^ http%2://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301] |
1) Should the code for requirements 3 and 4 be positioned before"removes index.php" code?
规则按照您编写它们的顺序进行处理。我希望您希望站点首先重定向然后执行删除索引,所以是的,它应该在
之前
2) Does the position have any bearing on the redirects that will be coming from the old site e.g. www.example-old.com/some-link-here.asp will be redirected to www.example-new.com/some-new-link-here
如果您对两个站点使用相同的目录,那么您需要在一个站点的所有规则前面加上 RewriteCond 来限制域。否则,规则的顺序(旧站点与新站点)很重要。
您从旧网站的重定向也应该包含新的规则,例如确保您在必要时访问 http/s 以避免额外的重定向。
下面是强制http/s的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 | #if not secure RewriteCond %{HTTPS} off #and starts with /extranet RewriteCond %{REQUEST_URI} ^/extranet [NC] #redirect to secure RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] #if secure RewriteCond %{HTTPS} on #and does not start with /extranet RewriteCond %{REQUEST_URI} !^/extranet [NC] #redirect to http RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] |