.htaccess MOD_REWRITE specific cases - ajax

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

Related

Silly 404 page usage?

Alright, so the only way I know of changing links to not show the file extensions;
this yourwebsite.com/customlink.php to yourwebsite.com/customlink
is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.
My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.
I apologize in advance if this is really stupid
As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.
Put an .htaccess file into the root of your site with the following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It will internally rewrite everything without an extension to the corresponding php file, provided that:
the request is not a valid directory
a file with a php extension is in fact present
A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.
Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.
It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.

Laravel infinite loop

I'm building an app using Laravel v.4.2. In the app/public directory, I have images folder which contains static images. Then, I created an ImagesController for users to manage assets. However, when I entered the URL: myapp.dev/images into browser, I got an infinite redirect loop error. I double checked all my routes and noticed there're no ones related to images path. Even when I commented out all routes entries, the error still exists.
I found a work around to this by rename the controller, however this is not going to be an ideal solution.
What can I do to completely deal with this error?
You will have to rename either your images directory, or your images route. What's happening is expected behavior of your server: when it hits the public directory (which is where all Laravel requests begin), it's finding that the images folder exists, so your URL is not redirected to index.php and your application is not launched. So it never even gets to your routes.
The cause of the loop itself depends on the contents of your .htaccess file, but it is probably happening because when you request myapp.dev/images, your server recognizes that images is a directory and immediately returns a 301 redirect to myapp.dev/images/ (with the trailing /). Then, your .htaccess file jumps in and tries to convert it back to myapp.dev/images, without the trailing slash. This happens in the line RewriteRule ^(.*)/$ /$1 [L,R=301].
Normally, when navigating to a folder in app/public, you should get a 403 Forbidden error. The line that does this is Options -Indexes, which disables the ability to display a list (or index) of directories in the browser. Usually, you have indexes disabled server-wide, in your server's httpd.conf file. You might want to check that that's the caseā€”or at the very least, add Options -Indexes to your .htaccess file in app/public.
Also, make sure that your .htaccess file in app/public contains both of these lines:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

.htacces redirect for AJAX search engine indexing

Ok, so I made a pure html/javascript AJAX website, but I want my pages indexable by Google.
I have my content files with meta information in plain html, but without menubar etc. and I have my index.htm with all the menubars, javascript AJAX stuff, etc.
To make AJAX indexable for google, my URL's should look like "<something>#!<somthingelse>", which Google indexbot will change to "<something>?_escaped_fragment_=<somethingelse>", such that my server knows it should return the content directly, instead of the page that loads it via AJAX.
However, since my server is stupid, as it doesn't use server side processing, I need to perform a trick via htaccess (which is where I fail :( )
The idea is as following:
I have my fancy URL's http://mysite.com/page1#!1, http://mysite.com/page2#!1, etc.
Normally, htaccess should rewrite that to /index.htm?page=page1 such that my AJAX reads the URL param and automagically loads page1.htm content file.
For Google indexer, it should ignore this rewrite for any url containing "?_escaped_fragment_=1" such that the url points to the content page directly
This way I have to make a small compromise by putting #!1 in every fancy URL, but as far as I can think of, it is the only way to do this without server side processing (except for htaccess of course)
I just cant seem to get the rewrite rules to do this.
Here's what ive come up with so far:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|.*&)_escaped_fragment_=1(&.*|$)
RewriteRule ^(.*)$ %1 [L,R=301]
RewriteRule ^(.*)$ /index.htm?page=%1 [L,R=301]

mod rewrite all url's except includes (js/css/img/some php)

What I want is a Wordpress type of URL rewrite.
What I have now is:
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
This almost works rewriting everything except existing files (css,js etc files are not rewritten as expected).
The problems I have with this are:
.css, .js, Image and PHP files are also accessible by entering their file name. eg.: domain.com/style.css will be accessible by anyone.
I want some existing files(only php right now) to be redirected anyway. eg.: domain.com/movies.php should redirect to rewrite.php(include 404.php) and domain.com/movies or domain.com/movies/ would include movies.php page.
Ideally I would also be able to change user entered URL from domain.com/movies to domain.com/movies/ for consistency more than anything else.
I want to keep .htaccess to a bare minimum. Does wordpress rewrite everything including .css files?
What I want:
Some php files to redirect others not. eg: includes/func.inc.php
should not be accessible to the users, while movies.php should be
accessible but ONLY from this url domain.com/movies/
(not essential)
Change url to canonical url eg: domain.com/movies to
domain.com/movies/ (some resources on how to achieve this would be
nice). Note: domain.com/movies url should still work but appear with
a slash at the end either via rewrite or maybe by just adding a
slash with javasript (faster?)
Make .css/.js files inaccessible by the user. eg: domain.com/style.css should redirect the user to a 404 page

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