【问题标题】:Use .htaccess to rewrite address from xxx.example.com to example.com/xxx/使用 .htaccess 将地址从 xxx.example.com 重写为 example.com/xxx/
【发布时间】:2023-03-04 00:40:02
【问题描述】:

我正在寻找一种方法,以便用户可以通过键入 xxx.example.com 访问他们的页面,然后转发到他们各自的 WordPress 帖子。

现在我的网站上已经有了两个重写功能:(不是我写的)

到处使用 https:

 #Rewrite everything to https
    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

还有 WordPress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

现在,WordPress 功能似乎将example.com/post_title 翻译成相应的帖子。

但是我该如何重写它以便将post_title.example.com 转换为相应的帖子?

如果可能,仍然显示xxx.example.com 而不是example.com/xxx

【问题讨论】:

  • 没人知道,还是不可能?
  • 好吧。 Domain.com 将打开根目录,xxx.domain.com 将访问 domain.com 中的目录 xxx。还是我搞错了?

标签: apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

以下应该可以工作,将您的.htaccess 更改为:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# Rewrite to https
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]

# This rewrite rule will only be used if
# we have an empty query string and the http host does not start with www
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com
RewriteRule ^$ http://%{HTTP_HOST}/%1 [L]

# The default wordpress rewrite rule
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

为此,我想您需要在您的 wordpress 安装中设置正确的永久链接结构:

【讨论】:

  • @Jerry - 你试过这个吗?
  • 搞笑,其实我刚来得及坐下,现在就去测试一下……
  • 第一个(没有虚拟主机(不知道那是什么))在连接到主站点时导致循环..
  • 遇到 www 问题尝试访问 site.com 时,它会尝试打开 site.com/www
  • @Jerry - 您可以轻松添加处理 www 部分的条件。我会更新我的答案。
猜你喜欢
  • 1970-01-01
  • 2014-03-04
  • 1970-01-01
  • 2019-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多