Ajax response call, 404 Not Found - ajax

i tried to use wordpress and codeigniter for a intelligent join between the best CMS and the best PHP Framework. Actually i intended to use codeigniter for database managemnent and wordpress for display. But i wanted to use codeigniter inside wordpress. So, i have a file tree like that:
www
- codigniter-core folder
- .htaccess
- wordpress-core folder
- .htaccess
- index.php
How can i make the wordpress .htaccess to cover also the codeigniter ant not trowing 404 error in jquery ajax call. My current wordpress .htaccess file it's:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^/codeigniter-core/
RewriteRule ^(.*)$ /index.php/$1 [L]
I also tried to ask this to Wordpress Forums and Codeigniter Forums, but no answer. I think it's hell of a rewrite condition or rule, but i don't know ...

I had this problem when creating a controller file that I would call via the jQuery ajax method. This controller file included wp-blog-header.php.
When you include the wp-blog-header.php in a file outside of WP framework WordPress does a check on the current file, and if it is not in its database it returns a 404. The work around is to add a 200 status header after the wp-blog-header.php.
header("HTTP/1.1 200 OK");
Now (jQuery) ajax to the file returns properly without a 404.

This worked great... can't tell you how long I was looking for this answer. I suspected this had to do with a header error because firebug showed the response but the header had a 404. Thanks this saved me a lot of time.
BTW I used this to fix the Quick Search plugin by Giulio Ganci
Just open the "search.php" file in the plugin folder and add
header("HTTP/1.1 200 OK");
Right after the first line of code. When your done the first few lines of code in this file should look like this
<?php
require('../../../wp-blog-header.php');
header("HTTP/1.1 200 OK");
if (isset($_GET['s']) && trim($_GET['s']) != '') {

Related

rewrite url only and stay on the same page

I have read a lot on stack about rewriterule and how it applies and I've tried reading up on some good articles online but I still cannot wrap my head around a few things.
I have blogs setup where all folders are in
https://domain.ca/posts/post-tree/*
So I've setup htaccess like this
RewriteRule ^posts/post-tree/(.*)$ /index.php?$1 [R=301]
As I'm sure you can guess this basically brings me root index.php where I catch this request with a $_GET to know the name of the blog folder it was requesting.
This is fine I can hit index.php and with $_GET I know the blog page they requested.
What I do not get, and I've tried a lot of things, is once I have this request in index.php how do I re-write the URL to show something like https://domain.ca/blogpage/ instead of looking like https://domain.ca/index.php? where https://domain.ca/blogpage/ does not really exist of course, but it is because I want to hide the http://domain.ca/posts/post-tree/ path.
Its a little like when wordpress processes a blog page with the id and after rewrites the url to whatever slug is set for that blog page. at least my understanding of it as they don't have individual folders for blogs, but I do.
I finally got this working with the following in the htaccess file
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# the above checks if file or folder exists, if not the below is processed
# this will route to base index file and fetch $1 folder via $_GET
RewriteRule . /posts/post-tree/index.php?$1

I want to block assets folder path in Codeigniter

Am new in Codeigniter i want to block assets folder in my website. I mean website users(website viewer) enter http://www.example.com/assets it move to 404 page.
Please help me...!
Thanks in advance
If your assets folder is there to serve things like CSS, JavaScript and image files (like would be intended), and is an actual directory, then you can either do this by adding a .htaccess file to the directory (assuming it's running under Apache) which will take all requests which are not for explicit file paths and route them to the website controller/action for a 404 (or other page you use for this).
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://yourdomain.com/path/to/404 [QSA,L]
</IfModule>
Alternatively, you can add an index.php file which redirects the browser to the location for a 404 page, or displays a 404 type page as a response.
header("Location: http://yourdomain.com/path/to/404");

.htaccess MOD_REWRITE specific cases

I am really struggling with .htaccess since I can't figure out how the syntax actually works and what the commands are. Let me describe what I need to achieve and maybe someone could guide me to the right answer:
I have a website which works with JQuery Ajax. So there is one single index.php file in the root folder. Any request of a sub-page via internal link(mysite.com/contact.php) is going through Ajax and the content information gets loaded from a folder called "/pages/" to the index.php.
But if a user enters the url itself (mysite.com/content.php) Everything breaks since the directory actually doesn't exist. Remember the content files are inside /pages/. And a direct access to the raw content files would not display the site, but only the information in raw form.
To solve this I started using .htaccess to pass not existing directory request to php, which passes it to Ajax. But here I got stuck. I am really bad with .htacces. This is what I got from the internet:
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works for the most part, except if the request goes through a subfolder (mysite.com/download/version1)
So here is what I need .htaccess to do:
Don't care if it is a folder or a file request, pass it to php.
(optional) Do not allow direct access to /pages/ folder, but do allow if request comes via Ajax
Do not mess up request calls for resource files. (This is important. When I fiddled around, I got many errors since .htaccess was also interfearing in resource file (.css/.js/.png) calls)
Feel free to take a look at the website trough: GitHub
The php part is inside index.php. The Ajax is done in PageHandler.js

Code Igniter URL routing confusion

I have a slight issue. I have configured my default controller as :
c$route['default_controller'] = 'news';
so when I point my browser to http://localhost/mysite all the news get loaded as instructed by the news controller.
Now on this page, I have given links to "read article" to read the full article. For this I have to point my browser to http://localhost/index.php/news/view/news-slug. I want to remove index.php from the url. I had already tried re-routing using wildcard without any success. Can you tell me how to do that?
There is a documentation on removing the index.php from the URL if you are using Apache with it's mod_rewrite:
Removing the index.php file
Using the following rewrite code:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Also, there are many similar questions to yours, have a look at these questions from the search:
Search query on removing index.php in Codeigniter
You need to define your .htaccess file for this. Check this wiki: http://codeigniter.com/wiki/mod_rewrite

How to redirect url to another

OK...
I have setup things so that when the following page is requested (browser users and servers)
http://www.visualise.ca/?_escaped_fragment_=corona
the website returns the following the content of this (HTML snapshot)
http://www.visualise.ca/corona
Where 'corona' always change, it varies depending on the page the users or servers are requesting. It could also be
http://www.visualise.ca/?_escaped_fragment_=anne-au-cherry
redirecting to
http://www.visualise.ca/anne-au-cherry
Thanks
UPDATE: OK let me be more clear. I use AJAX to load my Wordpress post and they appear like this http://www.visualise.ca/#!/corona when loaded. But it's not crawlable by Google that request to serv them as http://www.visualise.ca/?_escaped_fragment_=corona so I modified Wordpress to do so. Now Google can crawl my page and index its content and accessing the HTML snapshot available at http://www.visualise.ca/corona.
The problem is that when I paste the http://www.visualise.ca/#!/corona link to facebook it seems to read the http://www.visualise.ca/?_escaped_fragment_=corona and is unable to read the content. But when I paste directly the http://www.visualise.ca/?_escaped_fragment_=corona link it works, it reads http://www.visualise.ca/corona (The HTML).
So I thought maybe if I could redirect http://www.visualise.ca/?_escaped_fragment_=corona to http://www.visualise.ca/corona it would solve my problem.
Here is the existing .htaccess file
#--- DH-PHP handlers ---
AddHandler fastcgi-script fcg fcgi fpl
AddHandler php-fastcgi .php
Action php-fastcgi /cgi-bin/dispatch.fcgi
# 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
The common mistake that a lot of people do is trying to match whole URL including query string. The reality is: when matching URL, the pattern get applied to path part of it and query string has to be matched separately.
Use this rule: it will issue 301 Permanent Redirect from this kind of URL /?_escaped_fragment_=corona to /corona (where corona can be anything).
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=([^&]*)
RewriteRule .* http://%{HTTP_HOST}/%1? [R=301,L]
I guess your question is really "how do I do this kind of redirection?". So the answer is here:
There are three ways that I could think of, each with slight differences.
Doing the redirection server side
This basically means you set redirect headers in your response. In php, you could do this using the header function. It can also do a delayed redirect, in which case you need to worry about the contents of the page.
Doing it using client side using html's "http-equiv" meta tags. This way the page always gets loaded. Example here.
doing it via javascript. Thats you basic document.location.href thing. You need to figure out a way to pass the argument to javascript, or have your JS read it from the address url itself.
Since I've shown you 3 ways of doing this, I really hope that's what you're looking for :P
Update after seeing the comments:
The above methods will cause the URL to change. If you don't want the URL to change, but show the contents of that other page on your original page, you caould either do that using iframes (baaaad), or do the decent thing and set up URL rewriting.
:)
RewriteEngine on
RewriteRule \?_escaped_fragment_=(.*?) /$1 [L,R=301]
is it what are you looking for?

Resources