htaccess 中的 https 和规则顺序(使用表达式引擎)

发布于 2024-12-25 11:13:41 字数 987 浏览 1 评论 0原文

我正在表达式引擎中构建一个网站,其中一部分需要是 https。该网站还使用了新域名(新域名 www.example-new.com,旧域名 www.example.old.com)。

我想做以下事情:

  1. 删除index.php
  2. 强制www
  3. 强制https对于任何以www.example.old.com/extranet开头的URL
  4. 重定向https URL不是www.example.old.com/extranet(例如www.example .old.com/news 到 http

到目前为止,我有以下代码可以满足前两个要求:

    <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 -链接在这里

谢谢,

格雷戈尔

I'm building a site in expression engine that part of needs to be https. The site is also using a new domain name (new one www.example-new.com the old one www.example.old.com).

I want to do the following things:

  1. remove the index.php
  2. force www
  3. force https for any url starting www.example.old.com/extranet
  4. redirect https URLs that are not www.example.old.com/extranet (e.g. www.example.old.com/news to http

I have the following code so far that works for the first two requirements:

    <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

I seem to be going round in circles, so I've two questions that will help me write the other rewrites (although feel free to share suggestions...):

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

Thanks,

Gregor

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧城空念 2025-01-01 11:13:41

1) 从 ExpressionEngine URL 中删除“index.php”

RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

2) 添加“www”到所有 URI

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

3) 对任何以 /extranet 开头的 URI 强制使用 https://

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]

4) 重定向 https:// 不是 /extranet 的 URI

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]

将所有内容放在一起,这是您的完整集合重写规则:

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

1) Remove 'index.php' from ExpressionEngine URLs

RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

2) Add 'www' to all URIs

RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

3) Force https:// for any URI starting with /extranet

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/extranet(.*)$ [NC]
RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]

4) Redirect https:// URIs that are not /extranet

RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/extranet(.*)$ [NC]
RewriteRule . http://%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]

Putting it all together, here's your complete set of RewriteRules:

<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>
怼怹恏 2025-01-01 11:13:41

1) 要求 3 和 4 的代码是否应该放置在“removes index.php”代码之前?

规则按照您编写的顺序进行处理。我希望您希望网站首先重定向,然后执行删除索引,所以是的,它应该先进行

2) 该位置是否对来自旧网站的重定向有任何影响,例如 www.example-old.com/some-link-here.asp 将被重定向到 www.example-new.com/一些新链接在这里

如果您对两个站点使用相同的目录,则您需要为一个站点的所有规则添加限制域的 RewriteCond 前缀。否则,规则的顺序(旧站点与新站点)很重要。

您从旧站点的重定向也应该包含新站点中的规则,例如确保您根据需要访问 http/s 以避免额外的重定向。

下面是强制http/s的代码

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

1) Should the code for requirements 3 and 4 be positioned before "removes index.php" code?

The rules are processed in the order that you write them. I expect that you want the site to redirect first then do the remove index, so yes, it should go before

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

If you are using the same directory for both sites, then you would need to prefix all rules for one site with a RewriteCond that limits the domain. Otherwise, order of the rules (old vs new site) is important.

Your redirects from the old site should also incorporate rules in the new e.g. ensure that you are going to http/s as necessary to avoid extra redirects.

Below is the code to force http/s

#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]
迟月 2025-01-01 11:13:41
# 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]
# 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]
萤火眠眠 2025-01-01 11:13:41

如果有帮助的话...我昨天刚刚为一个电子商务网站做了这个...这是我的 .htaccess 文件

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

If it helps... I just did this yesterday for an ecommerce site... Here's my .htaccess file

# 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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文