Redirecting subdomains to wordpress content - wordpress

I have one single Wordpress application on domain www.mywpsite.com.
I have static pages:
a. www.mywpsite.com/subsite
b. www.mywpsite.com/subsite/child1
c. www.mywpsite.com/subsite/child2
and need that content to be respectively addressed using a subdomain like this:
a. subsite.mywpsite.com
b. subsite.mywpsite.com/child1
c. subsite.mywpsite.com/child2
Note that there is no physical structure for the subdomain, it is just a way of aliasing, and there is only one Wordpress installation under one document root. Also, I have a few pages and just one subdomain to work with, so a 'case by case' solution is valid.
Eventually I will also use the same subdomain for some dynamic content:
d. www.mywpsite.com/category/subsitenews => subsite.mywpsite.com/news
Finally, if possible, I need that the url retained in the address bar is the one using the subdomain, I mean:
The user types subsite.mywpsite.com/some-static-or-dynamic-content.
Redirection is performed to the appropriate wordpress content, and content is delivered.
The url in the user is still the same using subdomain, instead of the redirected url.
How can I do this?
I think I must use .htaccess but I have no idea how it works. I'm not sure if other questions I have found have to do with my problem.
redirect subdomain and retain url structure
.htaccess redirect ~ [fake-subdomain.domain.com/*] to [domain.com/*]
Thank you very much.

If the subdomain's aren't on the same server and under the same document root as the main domain, then you'll need to use a reverse proxy, or the P flag using mod_rewrite. You can try adding these rules to the htaccess file in your document root, preferably above any rules that may already be there:
RewriteEngine
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/([^/]+)/?$ http://$1.mywpsite.com/$2 [L,P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ http://$1.mywpsite.com/ [L,P]
The problem here is that dynamic content, ones that wordpress dynamically generates, will also be sent to the subdomains. There is no way to tell on the htaccess level what's valid dynamic content and what isn't. In order to check for that, you'll need to do it from within the wordpress CMS, probably some way of doing that but it'll require custom code or at least a plugin and possible customizing the plugin.

Related

Two WordPress Multisite installation in different subdirectories

I have a WordPress Multisite installation here: mydomain.com
I use it to have different languages like:
mydomain.com/fr
mydomain.com/de
...
Now we are changing design, but we would like to do step by step, language by language.
So we have installed another Wordpress Multisite in the folder /new, that it's working perfectly with sites like
mydomain.com/new/fr
mydomain.com/new/de
...
We would like to redirect JUST THE FRENCH language to the new wordpress. I've tried with this .htaccess in the root folder (based on the information in Wordpress documentation:
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteCond %{REQUEST_URI} !^/fr/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr(.*)$ /new/fr/$1
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^fr(/)?$ /new/fr/index.php [L]
But that is giving a "Error establishing a database connection". It's weird because, as I explained, the site mydomain.com/new/fr is working perfectly.
NOTE: we don't want to use subdomains, I know that would make a different approach and solution ;-)
EDIT: The error is because this query
SELECT blog_id FROM wp_blogs WHERE domain IN ( 'mydomain.com' ) AND path IN ( '/fr/', '/' ) ORDER BY CHAR_LENGTH(path) DESC LIMIT 1
I guess, the redirection is working well, but when the wordpress inside "new" directory tries to determine which blog to visit, it reads the URL and it's in "/" really, since the redirection rule has an [L] and keeps the original URL in the apache environment variables.
If you activated Multisite on WordPress 3.5 or later, you can follow the documentation setup in the section .htaccess and Mod Rewrite
According to the documentation:
.htaccess and Mod Rewrite Unlike Single Site WordPress, which can work
with "ugly" Permalinks and thus does not need Mod Rewrite, MultiSite
requires its use to format URLs for your subsites. This necessitates
the use of an .htaccess file, the format of which will be slightly
different if you're using SubFolders or SubDomains. The examples below
are the standard .htaccess entries for WordPress SubFolders and
SubDomains, when WordPress is installed in the root folder of your
website. If you have WordPress in its own folder, you will need to
change the value for RewriteBase appropriately.

How to properly configure DNS and Flexible SSL for Wordpress hosted on OpenShift through CloudFlare

TL;DR: I want to redirect https to http on all pages except for admin/login, where I want the exact opposite to happen. I also want www redirected to bare domain name. (UPDATE: Check Update 3 for the answer)
As is probably clear from the title, I have a Wordpress blog hosted on OpenShift for free. I have a custom domain bought from GoDaddy. I'm using cloudflare so I can have free SSL.
Here's my configuration:
CloudFlare DNS:
CloudFlare Page Rules:
This is what worked best. I actually wanted to have this rule:
*ghostlessmachine.com/* -> https://ghostlessmachine.com/$1
But I ran into even more problems like that, even though it seems to be pretty much what I'm supposed to do according to this CloudFlare article. Actually, initially I wanted to only force SSL in admin pages, but I didn't even know how to attempt that. I thought of using two page rules, like this:
*ghostlessmachine.com/* -> http://ghostlessmachine.com/$2
*ghostlessmachine.com/wp-* -> http://ghostlessmachine.com/$2
But I had no luck.
Here's my OpenShift configuration:
When I write ghostlessmachine.com in my address bar, it correctly takes me to https:.... I have shared a link, however (https://ghost...), and one person has reported not being able to access it. I couldn't reproduce locally.
When I try www.ghost..., I get:
This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS
Does anybody have any idea what I'm doing wrong? I've lost track of how many different configurations I've tried, but nothing seems to work.
Thanks!
UPDATE
OK, so following the advice in the comment I managed to get the situation a bit better. Still it's counter intuitive for me how the article I initially linked to just didn't get the job done while the other SO question did. So here's what I've changed:
Deleted the www.ghost... alias from OpenShift.
Changed CloudFlare's CNAME record from www -> blabla.rhcloud.com to www -> ghostlessmachine.com
Created this Page Rule: www.ghostlessmachine.com/* -> http://ghostlessmachine.com/$1
Now both ghost... and www.ghost... work and take me to http://ghost.... However, if I type https://ghost..., it also works without redirecting me to simple http. This is a problem.
I tried using this Page Rule instead:
ghostlessmachine.com/ -> http://ghostlessmachine.com/$2
So that I got https://, http://www, www, everything redirected to http://ghost..., but it doesn't work. I can't access my blog anymore and whatever address I try I get ERR_TOO_MANY_REDIRECTS.
UPDATE 2
Here's my full setup after all suggestions:
htaccess:
wp-config.php:
CloufFlare:
Result:
https -> http on non-admin/login pages: WORKING ✓
Trying to access admin/login pages: ERR_TOO_MANY_REDIRECTS
Update 3
This did the trick:
I still don't understand why this works and the rest doesn't though. This was basically a series of rather blind trial and error with some input from Allen here and Simon in the CloudFlare support page. In any case, all my requirements are respected now, thanks!
make sure following in your wp-config.php file:
define('FORCE_SSL_ADMIN', true);
define('FORCE_SSL_LOGIN', true);
look over here: Force non-WWW but force https across all of wordpress & the entire domain
for redirect everything else to non-https, you can add following into your root .htaccess file, before the wordpress rewrite:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/wp-admin.*
RewriteCond %{REQUEST_URI} !^/wp-login.*
RewriteCond %{HTTP_REFERER} !^https://.*
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Update:
CloudFlare's Page rule has following "Page rule priority is determined by their position in the list. If multiple rules match a URL, rules at the top take higher priority. "
let's see what happens before:
request to https://www.ghostlessmachine.com/wp-admin hit the first
rule, match found, then it goes to
http://www.ghostlessmachine.com/wp-admin!
now here comes http://www.ghostlessmachine.com/wp-admin, first rule,
no rewrite, goes down to 3rd rule, oops, it needs goto
https://www.ghostlessmachine.com/wp-admin!
this is how the loop comes

htaccess mod rewrite rule to over-ride cakephp controller

I have a site running cakephp and also a wordpress site together in the same root directory. I have the following issue. Wordpress is served requests first such that a url for the homepage will be routed through wordpress' index. Now our cakephp code includes a list of controllers that manage some special features of our site. We have a rewrite rule that looks as followed RewriteCond %{REQUEST_URI} !^/users. This allows urls like www.example_site.com/users to hit our cakephp framework instead of generating a 404 error when wordpress cannot find this page.
This rule bypasses the wordpress routing so that the request can be sent to cakephp. We are using a similar rule for each of our cakephp controllers. The issue is that we used to have a wordpress plugin that had a users page. This users page appended arguments to the url as followed www.example_site.com/users/some_dudes_username. Some links to these user pages are still indexed by google or in peoples bookmarks, so what is happening is the rewrite rule we wrote causes these to bypass wordpress (instead of 404ing there like it used to) and instead they hit cakephp and 404 there. I am thinking through how to make it so that any of our cakephp user controller functions are correctly redirected to (there are only a few so white-listing seems like the way to go), but any other request to the user controller for actions that will not exist like www.example_site.com/users/some_dudes_username would redirect to our FAQ page. I have the following white-list code which seems to work, but I cannot figure out how to correctly redirect everything else:
RewriteCond %{REQUEST_URI} !^/admin/users
RewriteCond %{REQUEST_URI} !^/users/login
RewriteCond %{REQUEST_URI} !^/users/admin_login
RewriteCond %{REQUEST_URI} !^/users/logout
RewriteCond %{REQUEST_URI} !^/users/admin_logout
RewriteCond %{REQUEST_URI} !^/users/admin_index
# Then I need a rewrite rule to take everyone else to the FAQ page.
Thank you to anyone that can help.
I am not sure I totally understand your issue, but it sounds like you need a RewriteRule like this:
RewriteRule ^/users/.*$ /URI/to/faq.html [R]
This rule should be right after your RewriteCond since the conditions only apply to the rule right after them.
Hope this helps.

mod rewrite exploding %{HTTP_HOST}

I have a situation I've not come across before that calls for some interesting mod_rewrite rules and I cant find any examples of someone trying to achieve the same thing in a similar configuration.
Currently I have two domain names which are configured to share the same document root, in said document root is a dynamic php application which, based on the incoming hostname, displays content specific to the that domain.
The domains for example purposes are:
www.example1.com
and
www.example2.co.uk
(one being a TLD the other not)
In addition to this application there are two wordpress installations one for each of the two domain names. As we are not using wordpress MU here I need some fancy rewrites to firstly hide the wordrpess folder, and secondly present the request to the correct folders based on the HTTP_HOST.
Currently I have the following:
RewriteRule ^wp-content(.*) wordpress/example1$1 [L]
RewriteRule ^wp-admin(.*) wordpress/example1/wp-admin$1 [L]
RewriteRule ^wp-login.php$ wordpress/example1/wp-login.php [L,R=301]
And similar rules for content specific pages.
This works well for the single wordpress installation, but obviously not for the second, what I was hoping to do here was something like the following:
RewriteRule ^wp-admin(.*) wordpress/${HTTP_HOST}/wp-admin$1 [L]
However I need to remove the www. and .com from the ${HTTP_HOST} variable (or the www. and .co.uk )
Any suggestions on a way to achieve this or a better approach would be appreciated.
You can use RewriteCond to check for a pattern in HTTP_HOST and then capture part of that pattern.
For instance:
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-zA-Z0-9_-]+)\.(?:com|co\.uk)$
RewriteRule ^wp-admin(.*) wordpress/%1/wp-admin$1 [L]
The RewriteCond directive above checks to see whether HTTP_HOST fits a domain pattern ending ".com" or ".co.uk" and optionally beginning with "www.". If it does, it captures the interesting part of the domain name.
Then the RewriteRule (which only fires if the RewriteCond does match) is able to refer to the captured part of the RewriteCond pattern by using the %1 back-reference.
The pattern I've used in the RewriteCond above might not suit your needs perfectly, but once you know you can use a back-reference to a pattern captured by RewriteCond, it should be easy for you to use this to get the effect you need.

Modifying CakePHP-resident Wordpress Redirects using mod_rewrite

I have a CakePHP site that contains a vanilla Wordpress installation. It lives in /app/webroot/blog/. The problem I'm having is a strange one.
I have the site configured, through mod rewrite, to redirect all requests to /app/webroot/blog to /blog/. The reason for this is that Wordpress was either throwing errors or displaying no content when the user visited it via the app/webroot path and it works just fine if you visit it at /blog/. It would also redirect the user to /app/webroot/blog if they attempted to visit /blog without a trailing slash, so we added that as well. Here are the mod_rewrite rules we're using to accomplish this:
RewriteRule ^blog$ blog/ [L]
RewriteRule ^app/webroot/blog/(.*)$ blog/$1 [L]
When the user attempts to visit the admin section of Wordpress but is not authenticated, it sends them to the /wp-login.php page and sends along a query string parameter called redirect_to that contains the URL the user was originally trying to access before being asked to authenticate. This URL contains the full path (/app/webroot/blog/...) instead of just going to /blog, even if the original request was a page within /blog. If the user successfully authenticates, then the user is sent back to the login page as though nothing happened (no error messages, etc.).
I would assume that the second mod_rewrite rule listed above would rectify the issue, but it doesn't seem to be.
I hope I've explained this thoroughly, please let me know if I can provide additional information that I may have forgotten. Thanks!
have you tried checking wordpress options in wp database. there are two options you should make sure that are correct. siteurl and home. both should be http://yoursite.com/blog
What's your document root directory? Make app/webroot/ your document root directory and you won't need any additional rewrite rules, because CakePHP's default rewrite rules already have rules for existing files and directories:
So your /app/webroot/index.php will be you application entry point with address .../ ,
and app/webroot/blog/ will be wordpress entry point with address .../blog/
Default rewrite rules are enough for this configuration:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # this will go to your blog dir, if it exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] # this is CakePHP app

Resources