简体   繁体   中英

htaccess simulate subdomain to subdirectory in Wordpress

I need to develop a plugin that can write into the WP htaccess file and "simulate" subdomains.

I have a new WP installation with basic htaccess on example.com

# 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

Is there a way via htaccess that if I go to about.example.com I see the page example.com/about , without changing the URL (meaning that I still see about.example.com in the browser address bar)?

And also, is it possible that if I go to about.example.com/category1 I see the page example.com/about/category1 ?

I tried using the following code by adding it to the end of the htaccess, but it doesn't work:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?example.com.com$
RewriteRule ^(/)?$ about [L]

You should be able to do that using the following rule in your .htaccess :

RewirieEngine On

RewriteCond %{HTTP_HOST} ^about.example.com [OR]
RewriteCond %{HTTP_HOST} ^www.about.example.com
RewriteRule ^(.*) http://example.com/about/$1 [P] 

What this does is check for the address about.example.com , if the condition is met it should redirect to example.com/about/.... but keeping the original address.

The [P] flag causes the request to be handled by mod_proxy , and handled via a proxy request.

However, keep in mind that using mod_proxy can cause security issues:

Take care when constructing the target URL of the rule, considering the security impact from allowing the client influence over the set of URLs to which your server will act as a proxy. Ensure that the scheme and hostname part of the URL is either fixed, or does not allow the client undue influence.

Normally using .htaccess you cannot go from one domain to another domain keeping the original URL. It can be a security nightmare as mentioned above. However, since this is a subdomain it should work.

Make sure you clear your cache before testing this.

Alternative

You can use ProxyPass - which would be a more secure option. You can find more information on how to use this via the Apache Documentation via this link. But this would need to done on a level higher than .htaccess . Through the config files.

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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