htaccess mod rewrite rule to over-ride cakephp controller - wordpress

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.

Related

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

Fishpig Wordpress Magento - http://example.com/blog resolves to www.example.com rather than www.example.com/blog

I have a working Fishpig Wordpress Magento instance on a production server.
All the green ticks show it has succesfully configured and when visiting
http://www.example.com/blog
it works. However when I try and and visit
http://example.com/blog
it sends me back to the magento home page at www.example.com .
I have tried setting the url in wordpress to both http://example.com/blog and http://www.example.com/blog but in both situations it always sends me to the home page at site.com.
This is a concern because most experienced web users will not type www when trying to access a location.
My htaccess is as generated by Wordpress and looks like
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
#wp generated
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Please not the wordpress site files are located at site.com/wp and it is the fishpig extension which arranges the direct from the non existent /blog path.
First of all, to start of, one way to resolve the issue is to simply make your Magento website DEFAULT to not have the www. in the url.
In Magento, go to System > Configuration > Web.
In the "Secure" and "Unsecure" tabs, find "Base URL."
The value should look something like this:
http://www.site.com/
Replace ALL references to the domain so it shows:
http://site.com/
Unfortunately, there may be a lot of appearances of it throughout the site. You'll need to do the same thing elsewhere on the server.
If you really want your site/blog to say www., though, you'll have to properly forward the .htaccess file in Magento. I'm not too keen on how Magento's .htaccess file works, but I do know how to forward a url and keep all of its additional pages as well (however, Magento's setup may interfere).
The code should look something like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^site.com$ [NC,OR]
RewriteRule ^(.*)$ http://www.site.com$1 [R=301,L]
Again, I don't know how well this will work with Magento's set up, but I've successfully used the above code to redirect url's to the correct url I want. That code above was actually used on a wordpress site, though.
Check for Rewrite rules in Magento's .htaccess file and try putting this above it. Be careful, though; it is VERY easy to cause 500 internal server errors if you mess up the .htaccess file, so always keep a backup.
Try this out and I hope it helps.

Redirecting subdomains to wordpress content

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.

Does anyone have experience simultaneously running a Drupal and Wordpress site and redirecting some links from Drupal to Wordpress

This is a really weird question and I apologize:
I've been asked if it's possible not to import our blog from Wordpress to Drupal but just keep it in Wordpress as an archive and re-direct our users say from hostname/blog/... to hostname/wordpress/... when a URL matches the Wordpress URL pattern.
I've never heard of anyone trying this and I'm wondering about pitfalls and whether or not it's even possible.
Thanks!
D
This looks like it should be pretty simple to do. I plan to take advantage of the mod_rewrite module for apache:
We will just need to configure a .htaccess file to redirect URLs matching hostname/blog/pattern/ - where pattern fits a Wordpress URL blog pattern.
I use mod_rewrite on my Drupal site. Here's an example I use to redirect users to www.earldouglas.com when they access earldouglas.com:
RewriteCond %{HTTP_HOST} ^earldouglas.com [NC]
RewriteRule ^(.*)$ http://www.earldouglas.com/$1 [L,R=301]
You could try something like the following:
RewriteCond %{REQUEST_URI} /blog
RewriteRule ^(.*)$ /wordpress [L,QSA]

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