在共享主机上使用.htaccess将IP地址重定向到域名

时间:2016-04-23 07:04:08

标签: php apache .htaccess redirect mod-rewrite

我正在尝试使用.htaccess文件将我的IP地址重定向到我的域名,但它似乎并没有起作用。除此之外,我还想将我的https链接重定向到http网站。任何人都可以提供帮助。

我的.htaccess文件看起来像这样

Options -Indexes

ErrorDocument 404 /404.php
ErrorDocument 403 /

<FilesMatch "\.inc.php">
  order deny,allow
  deny from all
</FilesMatch>

## SEO Friendly URLs ##
RewriteEngine On

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


RewriteRule ^(admin)($|/) - [L]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]


RewriteCond %{HTTP_USER_AGENT} libwww-perl.* 
RewriteRule .* ? [F,L]

<IfModule mod_expires.c>
    ExpiresActive on

    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
</IfModule>

1 个答案:

答案 0 :(得分:0)

您无法在共享主机上执行此操作。共享托管的整个想法是,您与多个其他网站共享同一台计算机及其IP地址。除非您的托管服务提供商允许您“声明”此IP地址(只有他们可以帮助您),否则无法完成。如果可以这样做,那也意味着共享您的IP的所有其他网站所有者都可以做您正在做的事情。那么服务器将重定向访问者在哪里?

由于这个原因,更改.htaccess文件根本不会有任何帮助。托管服务提供商管理的服务器配置会否决此问题。某些托管服务提供商允许选择购买专用IP地址,或者通常如果您从他们那里购买SSL证书,则会附带一个。除此之外,你没有选择。