Home > Software engineering >  How to redirect home page on a specific site with WordPress Multisite
How to redirect home page on a specific site with WordPress Multisite

Time:06-03

I have a WordPress site and I installed Multisite. WordPress is at the root (like example.com/). My other sites (multisite) are like portfolio.example.com.

I don't want my home page to be my WordPress site. I want it to be the home page I created (with my CSS and my JS) and it is in a folder in example.com/index.

I tried to redirect with a Redirect 301 /index.php https://example.com/index and a Redirect 301 https://example.com https://example.com/index but it redirects all my index.php to example.com/index so even portfolio.example.com becomes example.com/index because of the WordPress structure.

How can I redirect only my home page example.com to example.com/index keeping portfolio.example.com to portfolio.example.com?

I hope this is clear... Thanks!

Here is my .htaccess (it's the one generated by WP my redirection line) :

# BEGIN WordPress
# Les directives (lignes) entre « BEGIN WordPress » et « END WordPress » sont générées
# dynamiquement, et doivent être modifiées uniquement via les filtres WordPress.
# Toute modification des directives situées entre ces marqueurs sera surchargée.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]


# Redirection d’une URL vers une autre
Redirect 301 /index.php https://mysite.au/index


</IfModule>

# END WordPress

CodePudding user response:

You can't simply redirect/rewrite index.php since that is the WordPress front-controller and all requests are written to it.

I would have expected you to want to internally "rewrite" the request, rather than externally "redirect" the request, and keep the user at the "homepage" URL, ie. example.com/, rather than expose the true location of /index/index.html.

You can use mod_rewrite to internally rewrite (not "redirect") direct requests (as opposed to rewritten requests) for the document root to /index/index.html.

Note that you should not manually edit the code between the # BEGIN WordPress and # END WordPress comment markers since WordPress itself maintains this block of code and will try to overwrite this later.

Try the following to the very top of the .htaccess file, before the # BEGIN WordPress comment marker.

# Rewrite the "example.com" homepage to "/index/index.html"
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(index\.php)?$ index/index.html [END]

# BEGIN WordPress
:

The condition that checks against the REDIRECT_STATUS environment variable ensures that only direct requests from the user are rewritten and not internally rewritten requests by the later WordPress rewrite.

However, the above allows the user to request /, /index.php or /index/index.html to access your custom homepage (ie. /index/index.html).

You should include a "redirect" before this that removes index.php (or index/index.html) if requested directly. The above rewrite can then be simplified. For example, try the following instead:

# Redirect to remove "index.php" or "index/index.html" if requested directly
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(index\.php|index/index\.html)$ / [R=301,L]

# Rewrite the homepage to "/index/index.html"
RewriteCond %{HTTP_HOST} ^example\.com [NC]   
RewriteRule ^$ index/index.html [END]

# BEGIN WordPress
:
  • Related