【问题标题】:.htaccess redirect to error page if port is not 80如果端口不是 80,.htaccess 重定向到错误页面
【发布时间】:2012-09-14 11:12:37
【问题描述】:

我正在通过 USB 记忆棒运行 portable server。问题是我的本地机器上也安装了 WAMP,而 Apache 不知何故在 Windows 启动时启动,因为一些我现在不记得并且无法更改的随机原因。我想在这种情况下准备我的便携式服务器,因此从进程中关闭 httpd.exe 并启动我的便携式服务器不是一种选择。无论如何,由于 httpd.exe 已经处于活动状态,我的便携式服务器的 WordPress 站点只能通过 localhost:81 访问 - 这是一个问题,因为 WP 站点非常依赖于 URL,我不想在 WP 上包含带有端口的 url数据库。

这是我想通过 .htaccess 做的事情:

  • 在除error.php文件之外的任何路径上检查端口是否不是80
  • 如果不是端口 80 重定向到 /error.php?code=port

它有可能优先于 WP 重定向或 URL 处理吗?

在 error.php 中,我提供了有关如何手动关闭 httpd.exe 等信息,以便我的家人和朋友可以访问便携式站点。它有点像一个画廊和日历应用程序,用于活动和其他类似的东西......

请帮忙?我根本想不通。我知道其他人可能还没有运行 apache,但我想为这种情况做好准备。

类似于以下内容,但以下内容不起作用。

# BEGIN WordPress
<IfModule mod_rewrite.c>
    <If "%{SERVER_PORT} = 80">
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </If>
    <Else>
    RewriteEngine On
    RewriteRule ^(error.php)($|/) - [L]
    RewriteRule ^(.*)$ /error.php?code=port [L]
    </Else>
</IfModule>
# END WordPress

【问题讨论】:

    标签: php wordpress apache .htaccess port


    【解决方案1】:
    #  <If "%{SERVER_PORT} = 80">
    RewriteEngine On
    RewriteBase /
    RewriteCond  %{SERVER_PORT} ^80$ 
    RewriteRule ^index\.php$ - [L]
    RewriteCond  %{SERVER_PORT} ^80$ 
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    #</If>
    #<Else>
    RewriteEngine On
    RewriteCond  %{SERVER_PORT} !^80$ 
    RewriteRule ^(error.php)($|/) - [L]
    RewriteCond  %{SERVER_PORT} !^80$ 
    RewriteRule ^(.*)$ /error.php?code=port [L]
    #</Else>
    

    【讨论】:

    • 正是我正在寻找的......但它不起作用。当转到 sample.local:81 时,它会继续加载类似无限的加载,然后转到空白的 sample.local。有什么想法吗?
    【解决方案2】:

    你可以试试这个 apache 配置

    RewriteEngine on 
    RewriteCond  %{SERVER_PORT} !^80$ 
    RewriteRule ^(.*) http://%{SERVER_NAME}:80%{REQUEST_URI} 
    
    RewriteCond  %{SERVER_PORT} ^80$ 
    RewriteRule .* http://example.com/index.php [L]
    

    如果第 1 段不是 80,则重定向

    第二个只是你可以尝试的替代方案,如果你只想匹配 80

    有关重写的更多信息, http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

    【讨论】:

    • 请参考原帖,我写的更详细。
    • 我认为最好不要触摸和修改 wordpress 的 htaccess 因为一些插件(w3totalcache,其他)和 wp 本身检查/验证其重写/htaccess。在升级 wordpress 或其永久链接设置时,您可能会遇到一些问题。让我在我的本地主机上重新创建你的情况,然后我会更新我的答案
    • @fedmich 修改 WordPress 网站的 htaccess 是完全合理的
    【解决方案3】:

    VirtualHosts 是你要找的。​​p>

    一个小例子,说明如何实现http://wplocal/ http://wp.local/ 指向/var/www/wp AND http://localhost/ http://http.local 指向@ 987654328@.

    NameVirtualHost *:80
    
    <VirtualHost *:80>
        ServerAdmin wp@you.com
        DocumentRoot "/var/www/wp"
        ServerName wplocal
        ServerAlias wp.local
        ErrorLog "/somepath/here"
    </VirtualHost>
    
    <VirtualHost *:80>
        ServerAdmin you@you.com
        DocumentRoot "/another/root/path"
        ServerName localhost
        ServerAlias http.local
        ErrorLog "/logs/path"
    </VirtualHost>
    

    请参阅 this 了解解释这些内容的精美图片。

    希望有所帮助。

    【讨论】:

    • 请参考原帖,我写的更详细。 Server2Go 自动生成虚拟主机并更改端口。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 2021-12-20
    • 2012-10-09
    • 2021-05-28
    相关资源
    最近更新 更多