将服务器 IP 子目录重定向到 apache 上 .htaccess 文件中的主机名

发布于 2025-01-14 01:45:24 字数 1961 浏览 2 评论 0原文

我正在尝试使用 WordPress .htaccess 文件上的 mod_rewrite 将我的服务器 IP 重定向到我的主机名。

这是我尝试过的 -

  <IfModule mod_rewrite.c> 
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^X\.XXX\.XXX\.XX$
    RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]
  </IfModule>

如果我直接访问 IP 地址,则效果很好,但如果我使用子目录路径访问 IP,则它不会重定向。

例如:

IP XX.XX.XX.XX - Redirects fine
IP XX.XX.XX.XX/phpmyadmin - NO redirection

任何人都知道我的查询有什么问题。任何帮助表示赞赏。


更新:

这是我的.htaccess 文件,它实现了@MrWhite 的解决方案,但它仍然无法正常工作。

RewriteCond %{HTTP_HOST} !=www.XXXX.co.uk
RewriteRule (.*) https://www.XXXX.co.uk/$1 [R=301,L]



# BEGIN WP Security
#AIOWPS_BASIC_HTACCESS_RULES_START
<Files .htaccess>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
ServerSignature Off
LimitRequestBody 10485760
<Files wp-config.php>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
#AIOWPS_BASIC_HTACCESS_RULES_END
#AIOWPS_DISABLE_INDEX_VIEWS_START
Options -Indexes
#AIOWPS_DISABLE_INDEX_VIEWS_END
# END WP Security

#AIOWPS_BASIC_HTACCESS_RULES_END
# END WP Security

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Options -Indexes

I am trying to redirect my server IP to my hostname using mod_rewrite on my WordPress .htaccess file.

This is what I've tried -

  <IfModule mod_rewrite.c> 
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^X\.XXX\.XXX\.XX$
    RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]
  </IfModule>

This works fine if I visit the IP address directly but if I visit the IP with a subdirectory path it does not redirect.

For example:

IP XX.XX.XX.XX - Redirects fine
IP XX.XX.XX.XX/phpmyadmin - NO redirection

Anyone have any idea what is wrong with my query. Any help is appreciated.


UPDATE:

Here is my .htaccess file having implemented the solution from @MrWhite, but it's still not working.

RewriteCond %{HTTP_HOST} !=www.XXXX.co.uk
RewriteRule (.*) https://www.XXXX.co.uk/$1 [R=301,L]



# BEGIN WP Security
#AIOWPS_BASIC_HTACCESS_RULES_START
<Files .htaccess>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
ServerSignature Off
LimitRequestBody 10485760
<Files wp-config.php>
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</Files>
#AIOWPS_BASIC_HTACCESS_RULES_END
#AIOWPS_DISABLE_INDEX_VIEWS_START
Options -Indexes
#AIOWPS_DISABLE_INDEX_VIEWS_END
# END WP Security

#AIOWPS_BASIC_HTACCESS_RULES_END
# END WP Security

# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Options -Indexes

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

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

发布评论

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

评论(1

幽梦紫曦~ 2025-01-21 01:45:24

如果我直接访问 IP 地址,则效果很好,但如果我使用子目录路径访问 IP,则不会重定向。

这听起来像是您将指令放在了错误的位置。如果您已将规则放在文件末尾,即 WordPress 代码块之后,则只有对文档根(主页)的请求才会触发您的重定向,因为 WP 代码块实际上会忽略对主页本身的任何请求(因为 mod_dir 触发了对 index.php(WP 前端控制器)的子请求。

但是,任何对“子目录”的请求,即。虚拟 URL 路径,在您的规则有机会被触发之前将被路由到 WP 前端控制器。

您需要将规则移至文件顶部,重要的是在 # BEGIN WordPress 代码块之前。

您不需要 包装器,也不需要重复 RewriteEngine On 指令,因为这将在稍后的 WP 代码块内的文件中发生。


RewriteCond %{HTTP_HOST} ^X\.XXX\.XXX\.XX$
重写规则 (.*)$ http://www.example.com/$1 [R=301,L]

您可以检查请求的主机名是否是您的规范主机名,而不是检查请求的主机名是否为 IP 地址。您还将捕获非 www(或 www)的请求,并且您不受 IP 地址的束缚。

例如:

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

CondPattern 上的 ! 前缀对表达式求反。 = 运算符使其成为字典字符串比较,而不是正则表达式。所以不需要反斜杠转义文字点。 (这也将使匹配 IP 地址变得更容易,例如 =203.0.113.111。)

This works fine if i visit the IP address directly but if i visit the IP with a sub directory path it does not redirect.

This sounds like you've put the directives in the wrong place. If you've put the rule at the end of the file, after the WordPress code block then your redirect will only be triggered for requests to the document root (the homepage) since the WP code block effectively ignores any request for the homepage itself (since mod_dir triggers the subrequest for index.php - the WP front-controller).

However, any request for a "sub directory", ie. a virtual URL-path, will be routed to the WP front-controller before your rule has a chance to be triggered.

You need to move your rule to the top of the file, importantly before the # BEGIN WordPress code block.

You do not need the <IfModule> wrapper and you do not need to repeat the RewriteEngine On directive, since this will already occur later in the file inside the WP code block.


RewriteCond %{HTTP_HOST} ^X\.XXX\.XXX\.XX$
RewriteRule (.*)$ http://www.example.com/$1 [R=301,L]

Instead of checking that the requested hostname is the IP address, you could instead check that the requested hostname is not your canonical hostname. You will catch requests for non-www (or www) as well and you are not tied to an IP address.

For example:

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

The ! prefix on the CondPattern negates the expression. The = operator makes this a lexicographic string comparison, not a regex. So no need to backslash-escape the literal dots. (This would also make it easier to match IP addresses, eg. =203.0.113.111.)

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