Query String Redirection
-
In PHP, I'm wanting to store a session variable based upon a link that's clicked. I'm wanting to avoid query strings on pages that have content. My current workaround is to have a link with query strings to a php file that does nothing but snags the variables via $_GET, stores them into $_SESSION, and then redirects.
For example, consider this script, that I have set up to force to a mobile version. Accessed via something like a href="forcemobile.php?url=(the current filename)"
session_start();
//Location of vertstudios file on your localhost. Include trailing slash $loc = "http://localhost/web/vertstudios/"; //If GET variable not defined, this page is being accessed directly. //In that case, force to 404 page. Same case for if mobile session variable //not defined. if(!(isset($_GET["url"]) && isset($_SESSION["mobile"]))){ header("Location: http://www.vertstudios.com/404.php"); exit(); }
//Snag the URL $url = $_GET["url"]; //Set the mobile session to true, and redirect to specified URL $_SESSION["mobile"] = true;header("Location: " . $loc . $url); ?>
Will this circumvent the issue caused by using query strings?
-
So you're saying that in the webpage file itself, at the top
if (isset($_GET["mobile"]){
$_SESSION["mobile"] = $_GET["mobile"];
$url = //Get the current URL, use RegEx to strip query strings
header("Location: " . $url);
}
?>If that's the case, and if in fact that's SEO friendly, I definitely like that idea!
-
The idea of setting a session variable to direct page content is a great one. I use this to show the difference between two CSS files when I am designing sites. This is also great for query strings that come in from SEM. Rand talked about this before where you capture the string parameters and then redirect to a clean URL so users have a pretty URL to link to.
Now looking at your code. I do not know why a PHP scripter wants to force a 404? If you are testing a URL string, then find someplace logical to send them : )
href="forcemobile.php?url=(the current filename)
I would have a Go Mobile link on the page more something like this at the footer.
Then do your SESSION. If not, ALL your links are going to be ugly and you are only cleaning up the URL. You can test for SESSION and mobile versions of browsers to redirect.
I hope that helps
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
SEO Redirect
If we have several hundred domain names currently using a park page, would we be better served having them redirect to our corporate homepage for SEO purposes?
Technical SEO | | mkessler0 -
Importance of 301 Redirects
Hello, I have been brought in at the last minute to consult for an e-commerce client who is about to relaunch their website. The site currently receives 8000 visits a month, 3100 of which are from organic search. They have a few thousand product pages. The web development firm they are using is changing all of the old product page urls and using 'search engine friendly' urls for the new site, which is expected to launch in a few weeks. However, they did not/are not planning on including 301 redirects from the old URLs. Other than simply stating 'this will be bad for your SEO', what would be a correct way of explaining to the client how much of a problem it will be if their new site launches without 301s. For example, is this a big enough issue to delay the launch of the site / get in a contract dispute with the web developer?
Technical SEO | | stageagent0 -
Wordpress and Redirects?
I want to update my permalinks - actually I want to change the URL's to fit the content and keywords better. I can choose "edit" the URL, but don't I need a redirect? I don't see any htaccess Plugin installed.......is that what I need to be able to change my URL's in Wordpress?
Technical SEO | | cschwartzel0 -
Website redirects
We consolidated websites. All the international sites have been brought under the roof of our mothership site based in the US: www.crisisprevention.com ... We mapped out all of the URLs and where they should be redirected. However, if someone types in, say, www.crisisprevention.co.uk it redirects to the mothership site, BUT the old URL hangs around no matter what page you navigate to. I feel like it has duplicate content ramifications or worse. I would like opinions on this, so I can take my findings to IT and figure out a solution. Here’s another example: http://www.positive-options.co.uk and another http://www.positive-options.com
Technical SEO | | spackle0 -
Redirects in site map
I have a site with the ace/sef ( creates friendly URLS) in a large data base site. It creates a site map dynamically. Yet I realize one issue which I am trying to think through. I recently changed my urls to include an ID number example: homepage/houses/1134-big-blue-house The prior url was: homepage/houses/big-blue-house the original url above redirects to the new one with the ID like I want. However the site map has both URLS in it which go to same page I am not sure but it seems rather stupid to have the new URL and OLD redirected URL in the site map. Yet beside stupid I am wondering if this is duplicate content and will cause a penalty from the google bot. What is your opinion ?
Technical SEO | | aimiyo0 -
Redirecting 404
Hi. I'm working on a wordpress site, which got some old deleted pages indexed and now shows a 404 (also in the results) As these old pages earlier got content and probably also some links pointing towards it, what would then be best practice to do? Should i make an 301 redirect? Make the 404 noindex?
Technical SEO | | Mickelp0 -
What would happen if 301 redirects were not in place
Good Morning from 14 degrees C sunny Wetherby UK 🙂 My question is please.... "When a new site is given a total makover ie old urls are re written to radically different ones I know if you dont set up 301 redirects the infamous 404 error page will rear its head. But i wonder if 301 redirects were not configured how long on average does it take google to index the new site and serp links finally point to the new site". Thanks in advance 🙂
Technical SEO | | Nightwing0 -
Rel canonical with index follow on query string URLs
Hi guys, Quick question regarding the rel canonical tag. I have lots of links pointing at me with query strings and previously used some code to determine if query strings were in the URL and if they were then not to index that page. If there weren't query strings then the page would be indexed and followed. I assume I can now use the rel canonical tag on each of these pages so the value goes to the proper URL minus any query string. However do I need to have the rel canonical tag above the index, follow tag on the page? So URL is site.com/page.html?ref=ABC meta robots is "index, follow" Rel canonical is "site.com/page.html" Does the order of the meta robots and canonical tag matter? Thanks in advance!
Technical SEO | | panini0