Evik James
Evik James

Reputation: 10503

How to rewrite a URL to lower case and NOT do a double hit on the server?

I am using ColdFusion 8.0.1 and jQuery 1.4.3.

I am developing a piece of code that makes and Ajax request to a CFC using jQuery. I noticed that each Ajax request I made was actually submitted to the server twice. (I observed this in Firebug.) The first hit was forced to lower case. The second hit, which was now in lower case failed due to the rewrite.

I asked my boss about it and he pointed me to some code in the application.cfm file that forced the URL to all lower case and then did a 301 redirect using CFHEADER. I commented out that code and my Ajax request worked just fine.

Still, for SEO purposes, we need to force all requests to lower case using the rewrite in the application.cfm or the in htaccess file.

I understand that it is a much better idea to perform this type of function in the htaccess file.

My question is whether url rewrite using the htaccess file actually loads the page twice just like CFHEADER does. I can't work on this code until Monday, and I'd like to walk in knowing the options and the best answer.

RewriteEngine On
RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond $1 [A-Z]
RewriteRule ^/(.*)$ /${lowercase:$1} [R=303,L]

Thanks for your help!

Upvotes: 2

Views: 1428

Answers (3)

Adam Cameron
Adam Cameron

Reputation: 29870

Evik, you'll only get a redirect if there are upper case letters in the URL, so given your AJAX request URL is entirely within your control, just make sure it's all lowercase to start with!

Further to this - and as an alternative - you could put some RewriteCond on the RewriteRule so that the redirect doesn't happen for your AJAX requests. These aren't the sort of requests that need modification for SEO purposes anyhow, so it's perhaps best to simply leave them be.

Upvotes: 1

Andrew Willis
Andrew Willis

Reputation: 2349

No, it doesn't double-hit the server, it just changes (rewrites) the URL it's requesting. Thats why, in my last question, when I needed to double-hit the server, the .htaccess alone couldn't do it, and I had to resort to PHP Headers.

EDIT: by double-hit the server I mean execute the script and load the page. It is my understanding that rewriting the URL with .htaccess would have the desired effect for you, as it doesn't return anything to the browser until all rewrite conditions have been met, it doesn't redirect the url, it just re-writes it.

The other way to do it would be in JavaScript, before sending the Ajax request, run something like this

ajax_url = 'http://ExaMple.CoM';
ajax_url = ajax_url.toLowerCase();

Which will convert the string to lower case.

Upvotes: 1

Shaun McCran
Shaun McCran

Reputation: 208

If you are using Google analytics you can force all statistics to record as upper or lower case in the account settings btw. I do something similar but i also use a lower casing regex in the GA code.

Upvotes: 0

Related Questions