Use history.pushState while ignoring/bypassing .htaccess (alternative solution welcome) - html

What I'm trying to do is have a single dynamic file that takes a parameter to affect content (/view.html?k=about) but uses history.pushState to change the URL to something more user-friendly (ki/about). In addition, anytime an AJAX call is made on content.html to load new content, it updates the URL according, (e.g. if products are loaded via AJAX, change URL to keywords/products).
My current solution is any path requested from ki is redirected via .htaccess to the view.html page. view.html then uses history.pushState to change the URL. As links are clicked, the URL updates. The problem with this, however, is it causes a infinite loop.
Here is my .htaccess file, residing in the /ki/ folder.
RewriteEngine on
RewriteRule ^(.*)$ /concept/view.html?k=$1 [R=permanent,L]
What can I do to get my desired result? If there's a way to achieve the same thing without the .htaccess file then that's acceptable too.

You are going to want to rewrite any url that goes in the form of ki/about to the /view.html?k=about behind the scenes.
history.pushState is only really meant to be used for web applictions like Spotify that don't reload the page but would still make sense to have the back button have some functionality.
That way, urls can be shared without giving 404 pages.
I have not tested this but I am sure you want something like this
RewriteRule ^ki/([A-Za-z]+)/$ /view.html?ki=$1
If the user types in the ugly url, they will still get to the same page no problem. But the pretty urls will direct users to the right webpage.
For more info you can go here.
http://www.yourhtmlsource.com/sitemanagement/urlrewriting.html

Related

HTML Fallback Page

Is there a way I can set a 'fallback' page?
For example, my technique for updating pages on my website is to firstly remove my original index.php and then replace it with a seperate index.html that reads "We'll be back soon!". This is to explain that all pages are down for maintenance (as I upload updated content).
This means anyone attempting to access my home page will be greeted with the maintenance message. The problem, though: if someone manually types in the URL of the page they are trying to access they would instead be resulted with a "Page not found" error.
Is there a way in which I can direct the user to index.html if they are attempting to visit a location that does exist - but just not right now (since I removed it to update content) ...if that makes any sense ;D
Assuming this is an Apache (or compatible) web server, place an .htaccess file in the document root directory (where your index.html usually goes) and put theses lines in it:
RewriteEngine on
RewriteRule ^ maintenance.html
This makes the web server respond to any and all requests with the maintenance.html file. Obviously, put your "under maintenance" web page there. You'd need to tweak that a bit if you wanted to serve images on that maintenance page, but this should get you going. See https://stackoverflow.com/a/20563773/476.

Routing in static html landing page

I have a small portfolio landing page based on js/css/html. No frameworks/CMS, just pure static html. Entry point is index.html file with content on English language.
I want to use translations on my site: index.ru.html, index.ua.html, but I don't want to see any *.html or index.ua in the address bar. User can change a language by buttons on top of my page.
How can I route:
http://mysite/en to display index.html - first enter to site
http://mysite/ru to display index.ru.html
http://mysite/ua to display index.ua.html
?
Also can I route to specific div/section html tag: user enter http://mysite/ru/contacts to display contacts section in index.ru.html? Scrolling page also must change url... is it real or not?
Maybe I need to use micro-framework for my small needs?
EDIT:
Found good example on this site - http://www.even.lv/
Try adding this to your Root/.htaccess :
RewriteEngine On
RewriteBase /
RewriteRule ^en/?$ index.html [NC,L]
RewriteRule ^(ru|ua)$ index.$1.html [NC,L]
This will redirect "/en" to "/index.html" and "/ru" to "index.ru.html".
Might be using bootstrap scroll spy you can take user to sepecific section. if you don't want url to be change in that case ajax will help you. with use of jQuery you can trigger select change function jQuery("#language-select").change(function(){ // your logic try ajax here to change portion on page. })
Make directories for each language and put the relevant pages inside.
The only mechanism you have that allows you to omit a full url is the ability of the web server to serve a "default" page, which in your case is an index.html
By the same token to support mysite/ru/contacts you would need a directory structure of:
mysite/
ru/
contacts/
index.html
In other words, with pure html pages and without using rewrites, you can accomplish your structure with directories off the webroot and creating many individual index.html pages.
The other option is to use rewrite rules like those available using mod_rewrite and the apache web server.
Those rules require a good working understanding of regular expressions.

htaccess ajax call and hide html from search engine

so i developed a simple website which is using ajax call to html files to display.
the issue is that the search engine like google find the html file, and when we click on the link we see a poor html file out of context of the website of course.
so im guessing i have some work to do with htaccess file to handle this but how ?
my idea is to use htaccess rewriterule to redirect any .html file to the main index.php file.
something like that :
- www.my.com/team.html will call www.my.com/index.php which will detect the query-string team.html and will proceed to ajax loading the corresponding html file.
how is that possible please ?
Generally all search engines respect the terms set in the robots.txt file. If you wish that files in a particular folder in your site should not be crawled, the robots.txt can instruct the search engine to obey.
If you wish to lern about it, here is the link: http://www.robotstxt.org/robotstxt.html
You may see http://www.google.co.in/robots.txt for example.

Rewritten htaccess URL being cleared by hash

I have a page with a RewriteRule like this:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?user=$1&language=$2 [QSA,L]
In the index.php page, I have several links pointing to hashes # of element ids. When the user lands on the page, he gets this nice looking url:
mydomain.com/username/en
Which points to
mydomain.com/index.php?user=username&language=en
The problem is, as soon as one of the links is clicked, the browser changes the url to
mydomain.com/#id
Isn't it supposed to change to something like this?
mydomain.com/username/en#id
I want my url to look like the one above, that seems to be the expected behavior of the page...
Any thoughts on this?
Nevermind... just found the problem
I was using JS to push history states and it ended up messing with the urls!

Load same page with different URLs?

I want to create a single page such as this:
http://www.mywebsite.com/special/index.html
But anything in the /special/ folder should be able to load the index.html page. For example, if you go to
http://www.mywebsite.com/special/another-page.html
It should still load the index page but not change the URL in the browser or to search engines. Basically, you should be able to go to any page in the /special/ folder, keep the URL the same as you enter, but always load the index.html page. Any ideas?
A 404 or 301 redirect wouldn't work because that changes the URL in the browser and to search engines...
Thanks in advance!
A 404 redirect would not help, but a custom 404 handler would:
error404.php:
<?php
include('path/to/special/index.html');
?>
Assuming .html is a static or PHP page. If it is something else, youse the equivalent construct of that environment.
Using apache mapping it should be possible. I don't how to exactly do that but this doc http://httpd.apache.org/docs/2.0/urlmapping.html probably has the answer.
It is possible to use patterns to map URL to filesystem locations.
I think (untested) something like this would work in an .htaccess file in the special directory if you have the ability to use rewrite rules:
RewriteRule ^.*$ index.html
You would have to symlink index.html in the special directory to the real index.html.
The ^.*$ just means (beginning of line)(any amount of anything)(end of line) - basically a wildcard; there might be a better way of writing it.