Redirect a URL with /#/ - php

Our current website is built using AngularJS and as a result has URLs with /#/in them such as http://www.website.com/#/termsofuse
Short-sightedly, the URL for terms or use has been hard coded into a mobile application and although this has been changed, some users still have older versions.
We're moving to a WordPress site (running on Azure) and the new URL for terms of use is http://www.website.com/termsofuse
The issue is that I want to redirect to the new URL if the old URL is used (from the app where it is hard coded in older versions) but I can't find a way to do this with the /#/ in the URL (otherwise I could use HTML/JS at the old URL to redirect).
I have tried searching for solutions on Google and here but although I'm sure someone has had this problem, I'm finding it hard to define the search terms in order to get valuable results.
I also considered posting this on WordPress stackexchange but it is not really a WordPress question, I'm assuming I'll need to use some other method.
Appreciate any ideas or advice. Thanks in advance.
So far I have learnt from responses that I probably need a JS solution and based on that I have found the below which looks similar (at least shows me how to isolate the fragment after the URL). Since my issue is very specific, and I only need to look for the specific fragment #/termsofuse could I use this code (with midifications) to look for that string and redirect based on that?
Checking URL fragment for a keyword

Sadly, this is not possible as everything after the # doesn't get sent to the server.
What many people do in this sort of situation is to use a javascript/ajax solution to load the page.

By your description, as your new web site is built on WordPress without AngularJs.
So it hardly could approach your need in a traditional way. As hashtag # is a client symbol which is never passed on serve, so IIS on Azure will not get the portion after # of the URL, also URL rewrite module won’t see it too.
So if possible to modify home page of your WordPress site, here is a workaround with using JavaScript get the portion after # of the URL and redirect to the right URL.
Here is the code snippet:
(function () {
console.log(window.location.href);
var url = window.location.href;
params = url.split('#');
console.log(params);
if(params[1]){
window.location.href = params[1];
}
})();

Related

PhP/MySQL - change the URL based on content pulled from MySQL [duplicate]

I have http://mysite.com/index.php.
And a sub menu
home => http://mysite.com/index.php
about us => http://mysite.com/about.us.php
products => http://mysite.com/products.php
But i want http://mysite.com/index.php to process every request, and just change the content using Ajax request. This way, the site only loads the content part, and is much faster and easy to navigate.
The problem here is SEO, because the only URL google will see is http://mysite.com/index.php and I would like to associate http://mysite.com/about-us to the About Us content, http://mysite.com/product to the Products content, etc.
I know I can do this with PHP just reading the URL and writing the Ajax on the fly, but doing so the whole page is going to be reloaded every time.
Is there a way to do this without reloading the whole page?
What I think I need is to have a regular anchor in the submenu, for exampel pointing to "http://mysite.com/contact-us" but when clicked, instead of opening this page, process the Ajax request.
And if this is possible, Google is going to see this as black hat probably, right?
Regards
Alex
HERE THERE IS A SOLUTION:
window.history.pushState(data, title, url)
Here Rob explains how it works, and you have a working example:
http://moz.com/blog/create-crawlable-link-friendly-ajax-websites-using-pushstate
you can't change the URL in the address bar without changing the page because to be able to do that I couldlet you visit me at http://www.imhackingyou.com/sucker but change the addressbar to read http://www.bankofamerica.com/login
This is a routing issue, not an AJAX issue.
If you were using another tool (cough ASP.NET MVC cough), you'd just add a route (and I'm hopeful there's a way to do this in PHP) that accepted URLS like
/home
/products
...
and routed them to, say,
/index.php?area=home
/index.php?area=products
This is typically accomplished with a rewrite engine when used outside of a good MVC or RESTful URL system. I use ISAPI Rewrite on IIS, but if you're working on the LAMP stack, I think Apache provides a module that provides the same capabilities. (Google .htaccess )
WARNING: RANT FOLLOWS
And, for what it's worth,
Avoid trying to write your entire application in JavaScript. The server's there for a reason. Part of your job as a web developer is to absorb as much of the work onto your server as possible. Browser performance and compatibility issues will drive you mad when you try to do everything on the client.
Avoiding postbacks makes sense in a lot of circumstances, but it's not a silver bullet that you should try to apply to every page. Usually it makes sense to load a new page when a link is clicked. It's what the user expects, it's more stable (since most of the infrastructure required is server-side) and it's not slower than an AJAX request to retrieve the same thing.
Rules:
NEVER break the back button. Without careful planning, most AJAX apps break this rule.
See rule #1.
This sounds like it should be accomplished with a rewrite engine, but assuming that you have a good reason to use AJAX, you can change urls with javascript by modifying the portion after the hash, or better yet, the hashbang:
window.location.hash = "#!about-us";
http://mysite.com/
http://mysite.com/#!about-us
http://mysite.com/#!products
For more info on the hashbang from an SEO perspective, check out http://www.seomoz.org/blog/how-to-allow-google-to-crawl-ajax-content
How does Shopify do it then? Go to their website, click on the Features link and you'll see the URL says:
http://www.shopify.com/tour/sell-online
Then click on any of the sub links and you'll see that the address in the URl changes without using a hash but there is no page flip.
I don't think they are using ajax to change the content because it all appears to be included in hidden divs on the page, but regardless, you can apparently change the URL using client side tricks.

Trying to incorporate members area from one site into another

I am working on a website where I need to use the login form to log into a different site and then bring the member's area from that site over to the one I am building.
I have access to both sites and can make changes on either one. I would incorporate the code from the old one directly but it is in ASP and I'm working with PHP.
Any ideas? The purpose would be for someone to login to the site through site A (no problem) then get the information from site B (no problem) and present it in site A (no problem if I use cURL to get the site and break it up then display it on the new one). The issue I get into is the links that are on the new site and gathered from the old site will still point to links on the old site. Maybe there is a way to dynamically generate those pages on the new site somehow? Or am I going about it all the wrong way?
It's essentially a proxy. You need to parse and rebuild the links in the html source code received from site B. There are no functions available for this, but there are numerous open-source proxy scripts you can take code from.
Glype should be open-source (site appears to be broken now unfortunately).
https://www.glype.com/
You need to split the links to change them depending on where do they point.
Links to css/js should be rewritten to point to the real url (note that ajax content won't work, because you can't do an ajax request to another website)
Absolute links to the real website should be changed to relative
Relative links should point at your website (ex. $new_url = '/viewpage/?page=' . urlencode($url);)
Absolute links to other domains (not the old website that you proxy) should be dealt somehow, think about what do you want to do with them exactly.

What is best for SEO when using Ajax, Hash URL or Direct URL or Both?

I'm making a website based fully on Ajax, but i'm still doubting about my SEO.
In my "a" tags href I put a Direct link to the content, but i don't redirect the user to it, instead i get the content by Ajax, then I change the Address bar with "window.location.hash".
If i send to Google my sitemap with the two links (the hash link and the direct link) is that going to be a duplicate content and hurt my SEO or not.
If you have any better way to do this (instead of my way) please feel free to tell me about it.
Thanks Guys
Google has a useful guide here:
Making AJAX Applications Crawlable
For full ajax sites, you'll typically see the navigation of the site reflected in url hashes:
#/about or #/faq and so on. When that is the case, google will index the content of those ajax responses if you prefix the url structure with an exclamation point (!). This is called a hashbang. so:
#!/about would actually get indexed by google.... also you can throw those types of urls into your sitemap (i think)

How to make static Dynamic pages?

In MyBB forums you must have seen that all those threads are stoed as forum.com/Thread-Name-of-the-thread
So now this is static right ?
So now i have a site which has
blog.com/search.php?=SEARCHED+TEXT
So now how do i save this search so that Google can find out this page on my site ?
Indirectly what i mean to say is how i can i make
blog.com/SEARCHED+TEXT.html
"So now this is static right?" No. Just because the URL doesn't end in .php or similar doesn't mean it's static. It's time for you to learn the wonders of mod_rewrite:
http://www.workingwith.me.uk/articles/scripting/mod_rewrite
Your first example isn't static at all. It's just using a tool to route the request based on the URL.
All you need to get the same functionality is to investigate URL Routing in PHP and implement it in your application as well.
If you want Google to index this search page you have to tell Google it exists, either through a Sitemap or by putting a link on your site that Google can crawl. Google did fill in forms in the past, but I am not sure if they still do and afaik, they only did on a selected few sites.
To make the search static, you have to render the page once and store it in a file. Whether you do that manually by simply calling up the file in your browser and then saving it or by means of a Caching System is up to you.

Assigning permalinks to a page in PHP (Without using htaccess redirect)?

This is a noob question I belieive, in a content management system as well as several other types of sites that work on submissions, once you submit a URL in a URL shortening website for instance, how do you use PHP to redirect to the appropriate URL without a 404 or without using an htaccess.
Based on what I've found in simple url shortening scripts online, an htaccess is always used to redirect 404s to a PHP file which process the URL and goto the specific page, how do you do this without an htaccess?
Another example would be any blog software, once you submit a post, if you goto the specific URL it retrieves the appropriate post without the use of an htaccess.
I hope I'm being clear, thanks.
You are talking about two different concepts here. One is "url rewriting" the other is "redirection".
Url rewriting is the process of transforming one URL into another, and it may involve or not redirection. This happens server-side, before PHP kicks in. In fact, PHP is not aware of anything. This is performed as htaccess directives. What you obtain is usually the transformation of a complex nested url into a simple url with query.
For example: /blog/2010/10/30 rewritten to blog.php?year=2010&month=10&day=30
This is a beautification, in the sense that PHP responds to the second URL, and you could skip entirely the url rewriting, which is just for the sake of search engines and URL usability.
All of this happens before PHP starts. Then PHP could make its own redirections, and this is done using a call to header("Location: ..."), or a redirection through javascript or as html meta header.
None of this involves any 404.

Categories