简体   繁体   English

使用.htaccess将地址从xxx.example.com重写为example.com/xxx/

[英]Use .htaccess to rewrite address from xxx.example.com to example.com/xxx/

I am looking for a way so that users can access their page by typing xxx.example.com and are then forwarded to their respective WordPress post. 我正在寻找一种方法,以便用户可以通过键入xxx.example.com来访问他们的页面,然后转发到他们各自的WordPress帖子。

Now I already have two rewrite functions on my site: (not written by me) 现在我的网站上已经有两个重写功能了:(不是我写的)

To use https everywhere: 要在任何地方使用https:

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

And the WordPress: 和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

Now, that WordPress function seems to translate example.com/post_title to the corresponding post. 现在,WordPress函数似乎将example.com/post_title翻译成相应的帖子。

But how do I rewrite it so it translates post_title.example.com to the corresponding post? 但是如何重写它以便将post_title.example.com翻译成相应的帖子?

And if possible, still showing the xxx.example.com instead of example.com/xxx 如果可能,仍然显示xxx.example.com而不是example.com/xxx

The following should work, change your .htaccess to: 以下应该有效,将.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>

For this to work I guess you need to setup a correct permalink structure in your wordpress installation: 为此,我想您需要在wordpress安装中设置正确的永久链接结构:

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM