How to use JavaScript & .htaccess for SEO? - javascript

How can I use only JavaScript and/or .htaccess to map a request for:
http://example.com/map/new_york
to:
http://example.com/map?city=new_york
Without the user seeing the URL redirect?
UPDATE:
Since there seems to be a lot of confusion here, I'll provide more detail. I want to accomplish Search Engine Optimization (SEO) for my map application. The map resides on my web server at http://example.com/map. I want to allow people to deep link to a city of their choosing, which is in the form of http://example.com/map/?city=new_york. Since I want SEO, the URL should instead look like: http://example.com/map/new_york.
Without using any type of server-side programming (PHP/Python/etc), how can I accomplish this use case with only JavaScript and/or .htaccess?

You cannot use JavaScript to do this.
However, you CAN do this via .htaccess, if you have mod_rewrite installed on the web server that is hosting your website.
See this link for creating "Rewrite" entries in your .htaccess file:
http://corz.org/serv/tricks/htaccess2.php
(there are plenty of resources on the internet to do this, search for ".htaccess rewrite"
UPDATED:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^map/([^/]+) /map?city=$1 [NC]
That should rewrite all requests to map/[ANYTHING] to /map?city=[ANYTHING]

var url = 'http://example.com/map/new_york';
var u = url.split("/");
document.write(u[0]+'/'+u[1]+'/'+u[2]+'/'+u[3]+'?city='+u[4]);
http://jsfiddle.net/NzXmd/

Related

Redirect a url along with .htaccess and re-write rules gone wrong

I am trying to redirect http://localhost/anay-bose to http://localhost/anay-bose/1338.html (actual and valid url) via .htaccess
Here's my rule:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9])$ /anaybose/$1 [R=301]
Redirect /anay-bose http://localhost/anay-bose/1338.html
Problem is it's redirecting to a url such as http://localhost/anay-bose/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html/1338.html
Lots of 1338.html while I just need one 1338.html
Looking for an insight.
You need to enhance it a little via RedirectMatch Directive
RedirectMatch ^/anay-bose$ http://localhost/anay-bose/1338.html

How to replace post ID with url slug

I have a DB table that saves my title as a slug (ie: this-is-my-title-slug) but I have no clue how to use it in the url.
Example ofor current url http://www.example.com/post.php?id=102
What I want is
http://www.example.com/this-is-a-slug.
You're looking at SEO urls. And a slug is something you would use as a name of an article on www.example.com/articles/article-title-slug for instance, what you probably mean is a permalinks which basically are SEO urls.
If you're running an apache server with PHP and you have a vhost setup then you can use that vhost to allow url rewriting (Apache mod_rewrite).
This allows you to place a file called .htaccess in the webroot of your project (e.g. same directory as root index.php file), within this file you can set up rules that allow such rewriting. What often happens is that you rewrite everything to the root index.php file and you capture the URI and parse the parameters by splitting it on the forward slash (/) character. This is basically a router idea.
The advantage of parsing the URI yourself is that you don't really have to do much in the .htaccess file which is much more complex to handle than building a router in PHP itself as well.
A good example of a .htaccess file can be found on this SO answer
about how to get going with that. There are also plenty of other tutorials to get started with this other than that answer.
DigitalOcean - How to use the .htaccess file
TutsPlus - The ultimate guide to .htaccess
It's been a while since I've done it myself but if you have questions, just ask them and I'll see if I can help but anyways, this should point you in the right direction.

about url rewriting and js extension with php

I'm asking about the ability if I can use .js file as .PHP extension for example :
<script src="exampl.php">/script>
and if this method causes any problem ?
the second question is, if I make a url rewriting for making the extension to .js, how can I block the direct accès to example.php, and allow it when the visitor go to example. j's
I'm so sorry for my bad English, please help me and thank's to everyone how will participate :)
Assuming you are using Apache as a web server it would be better to create a rewrite rule:
Create or edit .htaccess and add: RewriteRule ^example.js$ example.php [L]
For the second part you could check $SERVER_['HTTP_REFERER'] or use CSFR token method.

link without www not working properly when i have <base href> with www in it

If i put <base href="http://www.example.com"/> then if I go in my browser to WWW.example.com everything is fine, but when I go to example.com some things with Cascade style sheet files and java script files are screwed (for example icons in bootstrap don't show) until i click some other page on my site so it transforms to WWW.example.com/page and from there everything is fine... I have tried with <base href="http://example.com"/> and going to WWW.example.com and it is same just other way around. How can i fix this?
You can set the base tag to the respective sub-domain that the user has entered. Or you can just redirect all your requests to the www sub-domain with a simple mod_rewrite rule.
Going the first way, I believe you can do that with javascript, not 100% sure though. Try having this at the start of your code, having the base tag with www as default.
if(document.URL.indexOf('://www.')===-1){
var base = document.getElementsByTagName('base')[0];
base.href = base.href.replace('://www.','://');
}
The other way is to use mod_rewrite, which is easily done with .htaccess files in apache.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I personally think the second way would be the better choice. Firstly because I'm not completely sure that the javascript scenario will work at all, and even if it does, javascript is not ran on all browsers and especially on bots. So I'd advice you to use .htaccess. Good luck!
How about edit your DNS information to set that "example.com" link to the real ip and didn`t as a 301 redirect

use javascript to read a link resource without ajax

Not sure if it's possible but how do I read a resource from a url using javascript without ajax?
for example, the following url is a static text file containing json encoded text
http://mysite.s3.amazonaws.com/jsonencodedcontent.txt
I'd like to use javascript to read the content from above link, read the json content into a javascript variable.
I can't use ajax because of cross site and I have no control over amazon S3 domain.
anyway to achieve this?
Try #Ben's suggestion first. If for any reason that doesn't work in your case, here's two options I've both seen and used, which may or may not be available in your case (I'm providing two overly simplified examples just to clarify my points):
Create a server side resource that resides in your domain and retrieves and returns the cross site content for you:
<?php
die(file_get_contents('http://mysite.s3.amazonaws.com/jsonencodedcontent.txt'));
Use mod_rewrite (or something similar) to create a virtual resource in your domain that resolves to the remote content behind the scenes:
RewriteEngine On
RewriteRule ^jsonencodedcontext\.txt$ http://mysite.s3.amazonaws.com/jsonencodedcontent.txt [P]

Categories