Wordpress redirect subdomain to subdirectory - wordpress

Hi i have a wordpress site with two subdirectories:
example.com/forum
example.com/shop
I need to display example.com/forum on subdomain like this: forum.example.com, and simmilar with /shop
I try to do this by modyfing htaccess, but no results.
The code i was added looks like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^forum.example.com
RewriteRule ^(.*)$ http://example.com/forum/$1 [L,NC,QSA]
So the whole htacces looks like this:
RewriteEngine On
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]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^forum.example.com
RewriteRule ^(.*)$ http://example.com/forum/$1 [L,NC,QSA]
But this not working.
Also i have redirect my subdomains to subdirectories on the serwer, do i need to copy htaccess to those subdirectories?
Sorry for my bad english

Related

exclude subdomains from .htaccess for wordpress multisite

I have a plesk server and its configured to launch Roundcube on webmail.domain.com . Also I configured my Wordpress as a multi-site to react as with anysubdomain.domain.com, but it stops the webmail subdomain to work. How do I exclude that subdomain (webmail) from been cached by Wordpress ?
This is my .htaccess:
RewriteEngine On
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]
to EXCLUDE (sub)domains in your ".htaccess" - file, you would use something like:
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^webmail\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

htaccess redirect a page to subdomain

We are trying to redirect a single page from our parent domain to a subdomain:
http://foo.com/calendar
http://www.foo.com/calendar
redirect to:
http://events.foo.com/calendar
Using cPanel redirect, it gives us this:
RewriteCond %{HTTP_HOST} ^foo\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.foo\.com$
RewriteRule ^calendar$ "http\:\/\/events\.foo\.com\/calendar\/" [R=301,L]
However, the redirect is not working.
RewriteEngine On is included at the start of the htaccess file, as it is a WordPress site.
UPDATE:
Here is the entire htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
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]
</IfModule>
# END WordPress
RewriteCond %{HTTP_HOST} ^foo\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.foo\.com$
RewriteRule ^calendar$ "http\:\/\/events\.foo\.com\/calendar\/" [R=301,L]

Subdomain "m." with Wordpress and. htaccess (Conflict in the rewrite rules in the htaccess.)

I do not usually in the habit of posting , because I always find my answer by searching the forums , but today I came across a real problem !
I have a Wordpress install multisite in a subdirectory of my domain. That is to say, it is at http://mondomaine.com/fr/ .
I wish to make a mobile version in a subdomain "m . " I want my mobile version is available at http://m.mondomaine.com/fr/ .
My host has created the subdomain m that points to the root of the site.
When I want to access a page from my mobile subdomain redirects it necessarily to the homepage /fr/ .
I tried to change my .htaccess , But nothing could be done , or I have a redirect loop , or I have a 404 page.
I suppose that there is a conflict between the rules , but I really tried and tested lots of solutions and I found nothing conclusive.
The original htaccess generated by Wordpress and located in the directory /fr :
RewriteEngine On
RewriteBase /fr/
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
.htaccess that I modified:
RewriteEngine On
RewriteBase /fr/
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteCond %{HTTP_HOST} !^m\.(.*) [NC]
RewriteRule . index.php [L]
RewriteCond %{HTTP_HOST} ^m\.(.*) [NC]
RewriteRule ^(.*)$ http://mondomaine.com/fr/$1 [L,P]
Thank you in advance to those who respond
If mod_proxy is enabled then move last m. rule right at the top, just below RewriteEngine On as this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^m\.(.+)$ [NC]
RewriteRule ^(.*)$ http://mondomaine.com/fr/$1 [L,P]

Forward to www in WordPress multisites

I have a WordPress multisite (using subfolders, not subdomains) in the format domain.com. For reasons of consistency and better SEO, I'd like to forward it to www.domain.com.
I've tried putting this code into .htaccess but I get a redirect loop:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# this is the code I added to forward to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
I'm pretty sure my domain is set to without www in the WordPress settings but it won't let me see that any more now I have a multisite.
I would try first not using htaccess redirect and remove the www redirect you have. Then edit your wp-config.php and change the DOMAIN_CURRENT_SITE to be your www domain. This way wordpress will handle the redirection.
Or you can try the htaccess method, but you might have some order problems, I believe with MU, the redirect needs to be the first thing. Replace domain.com with your real site.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

Wordpress network .htaccess issue

I have a Wordpress network setup but currently there is only one subsite with the url
/psychology
In the future there will be addition sites with
/md
/vet
but for the time being there will only be /psychology
What I want to do is have a rewrite rule in my htaccess that when a user visits the root of the site (/) it actually loads the /psychology site.
I tried adding a line like this to my htaccess
RewriteRule (.*) psychology/$1 [R,L]
but it broke the site. here is my current htaccess (basically the wordpress default for a network install)
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /ASKanALLY/
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Any ideas?
You need to add the base to the RewriteRule destination. And add a condition so you don't get a redirect loop.
Adding these two lines after RewriteRule ^ - [L] should work:
RewriteCond %{REQUEST_URI} !^/ASKanALLY/psychology ## Not already viewing a psychology URL
RewriteRule ^(.*)$ /ASKanALLY/psychology/$1 [R,L] ## Redirect to psychology
Here's the complete example:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /ASKanALLY/
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/ASKanALLY/psychology ## Not already viewing a psychology URL
RewriteRule ^(.*)$ /ASKanALLY/psychology/$1 [R,L] ## Redirect to psychology
RewriteRule ^[_0-9a-zA-Z-]+/(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^[_0-9a-zA-Z-]+/(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>
Your RewriteRule didn't work because it was matching all URLs, including ones to /psychology/foo. I'm guessing you don't have a .htaccess file under /psychology?
Try this - it'll rewrite just the root.
RewriteRule ^/$ psychology/ [r=301,L]

Resources