Regex htaccess文件不是';t将所有链接重定向到https和IS';t隐藏url的某些部分
我一直在修改网站的htaccess文件,以执行以下操作:Regex htaccess文件不是';t将所有链接重定向到https和IS';t隐藏url的某些部分,regex,apache,.htaccess,http,https,Regex,Apache,.htaccess,Http,Https,我一直在修改网站的htaccess文件,以执行以下操作: 任何人都不应该访问基本url(example.com)和试图访问的任何人 访问它应该重定向到example.com/site/ 在所有链接(包括子文件夹和子文件夹中的URL)上强制使用https 在显示的url名称中隐藏/site/ Https重定向会发生,但我也可以通过手动将Https更改为http来访问任何页面的http版本。此外,我无法从地址栏中显示的url中隐藏/site/。不,我没有访问vhosts文件的权限 下面是我如何修改.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteCond %{REQUEST_URI} !^(.*)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^(.*)$ /site/$1 [L,NC,R]
</IfModule>
重新启动发动机
重写cond%{HTTP_HOST}example\.com[NC]
重写cond%{SERVER_PORT}80
RewriteCond%{HTTP_HOST}^example\.com$[或]
RewriteCond%{HTTP_HOST}^www\.example\.com$[或]
重写cond%{REQUEST_URI}^(.*)
重写规则^(.*)$https://%{HTTP_HOST}%{REQUEST_URI}[R=301,L]
重写规则^(.*)$/site/$1[L,NC,R]
任何帮助都将不胜感激
因此,所有内部链接都必须通过example.com/site/*
如果希望从URL中“隐藏”/site
子目录,则需要从所有内部超链接中实际删除/site
URL路径段。如果链接中存在/site
部分,则所有用户都可以看到它(在页面上和状态栏中-单击链接之前),因此它根本不“隐藏”
example.com
),任何试图访问它的人都应该被重定向到example.com/site/
/site/
/site
URL路径段(#3)。“重定向”将公开/site
子目录
事实上,每个人都应该访问基本URL<代码>/site不在URL中(已将其从所有内部超链接中删除)。然后在内部将所有指向基本URL的请求重写到/site
子目录。这完全是服务器内部的-用户不知道这一点。这不是外部“重定向”,而是内部“重写”
因此,实际过程是:
example.com/
和example.com/foo
)内部重写到/site
子目录(分别是/site/
和/site/foo
)/site
子目录的请求“重定向”回根目录。但是,只有当您已经从所有内部超链接中删除了/site
时,才应该执行此操作,否则每次单击都会从外部重定向用户,这对您的用户来说是“缓慢的”,并且会影响您的服务器。我们还必须小心重定向循环,只重定向来自用户的直接请求,而不是Apache重写的请求
如果您只有example.com
和www.example.com
(如您在评论中所述),则无需进行所有主机名检查(即HTTP\u HOST
)-这些检查也是重复的,因此您在这里不止一次地检查相同的内容
检查%{REQUEST\u URI}的条件^(.*)
将始终失败(“not anything”始终为false)。这仅仅是因为条件是或是,规则可以做任何事情
最后一个重写规则
无条件地将所有内容“重定向”到/site
子目录。这将公开/site
子目录。您还希望这会创建一个无止境的重定向循环,除非在/site
子目录中有另一个.htaccess
文件阻止这种情况发生
^(.*)$
-如果您没有在重写规则
替换中使用反向引用,则无需捕获URL路径。在HTTP-to-HTTPS重定向中,这可以简化为^
——因为它只需要对每个请求成功
请尝试以下操作:
RewriteEngine On
# 1. HTTP to HTTPS redirect (same host)
RewriteCond %{SERVER_PORT} 80
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# 2. Internally rewrite all requests to the /site subdirectory
# Except for requests that are already for the /site subdirectory - prevent rewrite loop
RewriteRule !^site/ /site%{REQUEST_URI} [L]
不需要
包装器
此外,还可以选择从外部将对/site
子目录的直接请求重定向回根目录(规范URL)。注意:仅当所有内部超链接都从URL中删除了/site
子目录时。这将先于上述指令
# 0. Redirect direct /site requests back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^site/(.*) https://%{HTTP_HOST}/$1 [R=301,L]
为了防止重定向循环,需要(可能)检查REDIRECT_STATUS
环境变量的条件,因为我们不希望根据规则2重定向已重写的请求
最好先测试302(临时)重定向,以避免潜在的缓存问题
在测试之前,您需要清除浏览器缓存
也考虑了WWW子域的规范化。
更新#1:要能够访问文档根目录中的其他文件或
/site
子目录之外的文件,则需要在规则#2中添加一个条件,以排除直接映射到文件的请求。例如:
# 2. Internally rewrite all requests to the /site subdirectory
# Except for requests that are already for the /site subdirectory - prevent rewrite loop
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^site/ /site%{REQUEST_URI} [L]
# 2. Internally rewrite all requests to the /site subdirectory
# Except for requests that are already for the /site subdirectory
# Or any [sub]directory...
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^site /site%{REQUEST_URI} [L]
更新#2:我现在无法访问example.com/site2
上受密码保护的目录。它重定向到/site
,并显示“未找到文件”
您可以只为这一个子目录创建一个例外。例如,按如下方式编辑上述规则:
# 2. Internally rewrite all requests to the /site subdirectory
# Except for requests that are already for the /site or /site2 subdirectories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(site|site2) /site%{REQUEST_URI} [L]
或者,为除根目录之外的所有目录设置一个例外,根目录
# 2. Internally rewrite all requests to the /site subdirectory
# Except for requests that are already for the /site subdirectory
# Or any [sub]directory...
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^site /site%{REQUEST_URI} [L]