www。通过.htaccess转发转发到index.php

时间:2014-10-12 10:30:01

标签: .htaccess

我最终要做的是将www的任何网址转发给非等效的www。因此https://www.baremetrics.io/academy会自动转发到https://baremetrics.io/academy

相反,这些www页面转发到https://baremetrics.io/index.php

我很难理解为什么。希望.htaccess文件中存在我忽略的内容。

以下是baremetrics.io的.htaccess文件的内容:

Header set Access-Control-Allow-Origin "*"

# Turn on the Rewrite Engine
RewriteEngine On

# If you're running in a subfolder (like http://example.com/statamic),
# add that here. E.g. /statamic/
RewriteBase /

# Protect your system files from prying eyes
RewriteRule ^(_app) - [F,L]
RewriteRule ^(_config) - [F,L]
RewriteRule ^(_cache) - [F,L]
RewriteRule ^(_content) - [F,L]
RewriteRule ^(_logs) - [F,L]
RewriteRule ^(admin/themes/[^/]*/(?:layouts|templates)) - [F,L]
RewriteRule ^(.*)?\.yml$ - [F,L]
RewriteRule ^(.*)?\.yaml$ - [F,L]
RewriteRule ^(.*/)?\.git+ - [F,L]

# This will prevent all .html files from being accessed.
# You may want to remove this line if you want to serve
# static files outside of Statamic
# RewriteRule ^(.*)?\.html$ - [F,L]

# Remove trailing slashes from your URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

# Remove the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

# Redirect old pages
RedirectMatch 301 ^/signup(.*)$ https://dashboard.baremetrics.io/signup$1
RedirectMatch 301 ^/dashboard(.*)$ https://dashboard.baremetrics.io$1
RedirectMatch 301 ^/stats(.*)$ https://dashboard.baremetrics.io/stats$1
RedirectMatch 301 ^/billing\.html(.*)$ https://dashboard.baremetrics.io/billing.html$1
RedirectMatch 301 ^/switch(.*)$ https://dashboard.baremetrics.io/switch$1

# No WWW
RewriteCond %{HTTP_HOST} !^baremetrics.io$ [NC]
RewriteRule ^(.*)$ https://baremetrics.io/$1 [L,R=301]

为了它的价值,该网站将部署到Heroku。

1 个答案:

答案 0 :(得分:1)

问题是您的其他规则首先发生,导致您的非WWW重定向不会触发。

您可以在# No WWW之后立即移动RewriteBase /规则,以便修复它。

Header set Access-Control-Allow-Origin "*"

# Turn on the Rewrite Engine
RewriteEngine On

# If you're running in a subfolder (like http://example.com/statamic),
# add that here. E.g. /statamic/
RewriteBase /

# No WWW
RewriteCond %{HTTP_HOST} !^baremetrics.io$ [NC]
RewriteRule ^(.*)$ https://baremetrics.io/$1 [L,R=301]

# Protect your system files from prying eyes
RewriteRule ^(_app) - [F,L]
RewriteRule ^(_config) - [F,L]
RewriteRule ^(_cache) - [F,L]
RewriteRule ^(_content) - [F,L]
RewriteRule ^(_logs) - [F,L]
RewriteRule ^(admin/themes/[^/]*/(?:layouts|templates)) - [F,L]
RewriteRule ^(.*)?\.yml$ - [F,L]
RewriteRule ^(.*)?\.yaml$ - [F,L]
RewriteRule ^(.*/)?\.git+ - [F,L]

# This will prevent all .html files from being accessed.
# You may want to remove this line if you want to serve
# static files outside of Statamic
# RewriteRule ^(.*)?\.html$ - [F,L]

# Remove trailing slashes from your URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]

# Remove the index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]

# Redirect old pages
RedirectMatch 301 ^/signup(.*)$ https://dashboard.baremetrics.io/signup$1
RedirectMatch 301 ^/dashboard(.*)$ https://dashboard.baremetrics.io$1
RedirectMatch 301 ^/stats(.*)$ https://dashboard.baremetrics.io/stats$1
RedirectMatch 301 ^/billing\.html(.*)$ https://dashboard.baremetrics.io/billing.html$1
RedirectMatch 301 ^/switch(.*)$ https://dashboard.baremetrics.io/switch$1