将站点移动到子域,.htaccess 重定向 Apache
Moving site to a subdomain, .htaccess redirections Apache
我在地址 http://example.com
有一个站点,它已配置为删除 url 末尾的 .html(.php 等)并防止热点linking 事件(搜索引擎除外)。
我想建立一个多区域站点,其中包含 http://us-en.example.com
这样的子域,并将所有内容从我当前的站点 http://example.com
完全移动到 http://us-en.example.com
。
我有几个关于 .htaccess
文件的问题:
问题 1
示例:
用户有一个像这样的 "clear" link http://example.com/images/all/image1
,但是由于我移动了所有内容,所以这条路径不包含图像并且 不存在 .
如何设置配置文件以将此 "clean" url 重定向到 "default" 子域 (us-en),从 http://example.com/images/all/image1
到 http://us-en.example.com/images/all/image1
,但如果 link 已经包含一个子域,则不执行任何操作?
问题 2
如何正确设置 .htaccess
文件以使所有子域具有与原始域相同的功能(删除 .html/.php 并防止 hot-linking)?
问题 3
如何从下面的模板中完成所有这些操作?
DirectoryIndex home.html
Options +MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)\.html$ / [R=301,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?example\. [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !bing\. [NC]
RewriteCond %{HTTP_REFERER} !yahoo\. [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
首先,如果您可以访问 apache 配置文件,则根本不应使用 .htaccess(详细说明请参阅 here)
Q1:
在 .conf 中:
<VirtualHost *:80>
ServerName example.com
Redirect / https://us-en.example.com/
</VirtualHost>
或者(如果您不想使用 .conf 文件),将其放在 .htaccess
之上:
RewriteEngine on
RewriteCond %{HTTP_HOST} =example.com
RewriteRule ^ https://us-en.example.com%{REQUEST_URI}
对于其他问题,您的规则中没有任何特定域会阻止它们被重复使用。您应该只更改(将规则从 .htaccess
移动到 VirtualHost 配置时)
RewriteRule ^(.*)\.html$ / [R=301,L]
至
RewriteRule ^/(.*)\.html$ / [R=301,L]
因为在 VirtualHost 上下文中,Pattern 最初将与主机名和端口之后(以及查询字符串之前)的 URL 部分匹配,因此您需要领导 /
那里
我在地址 http://example.com
有一个站点,它已配置为删除 url 末尾的 .html(.php 等)并防止热点linking 事件(搜索引擎除外)。
我想建立一个多区域站点,其中包含 http://us-en.example.com
这样的子域,并将所有内容从我当前的站点 http://example.com
完全移动到 http://us-en.example.com
。
我有几个关于 .htaccess
文件的问题:
问题 1
示例:
用户有一个像这样的 "clear" link http://example.com/images/all/image1
,但是由于我移动了所有内容,所以这条路径不包含图像并且 不存在 .
如何设置配置文件以将此 "clean" url 重定向到 "default" 子域 (us-en),从 http://example.com/images/all/image1
到 http://us-en.example.com/images/all/image1
,但如果 link 已经包含一个子域,则不执行任何操作?
问题 2
如何正确设置 .htaccess
文件以使所有子域具有与原始域相同的功能(删除 .html/.php 并防止 hot-linking)?
问题 3
如何从下面的模板中完成所有这些操作?
DirectoryIndex home.html
Options +MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)\.html$ / [R=301,L]
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g?|png)$ [NC]
RewriteCond %{HTTP_REFERER} !^https?://([^.]+\.)?example\. [NC]
RewriteCond %{HTTP_REFERER} !search\?q=cache [NC]
RewriteCond %{HTTP_REFERER} !google\. [NC]
RewriteCond %{HTTP_REFERER} !bing\. [NC]
RewriteCond %{HTTP_REFERER} !yahoo\. [NC]
RewriteRule \.(gif|jpe?g?|png)$ - [F,NC,L]
首先,如果您可以访问 apache 配置文件,则根本不应使用 .htaccess(详细说明请参阅 here)
Q1:
在 .conf 中:
<VirtualHost *:80>
ServerName example.com
Redirect / https://us-en.example.com/
</VirtualHost>
或者(如果您不想使用 .conf 文件),将其放在 .htaccess
之上:
RewriteEngine on
RewriteCond %{HTTP_HOST} =example.com
RewriteRule ^ https://us-en.example.com%{REQUEST_URI}
对于其他问题,您的规则中没有任何特定域会阻止它们被重复使用。您应该只更改(将规则从 .htaccess
移动到 VirtualHost 配置时)
RewriteRule ^(.*)\.html$ / [R=301,L]
至
RewriteRule ^/(.*)\.html$ / [R=301,L]
因为在 VirtualHost 上下文中,Pattern 最初将与主机名和端口之后(以及查询字符串之前)的 URL 部分匹配,因此您需要领导 /
那里