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
-
Using Temporary Redirects for Ecommerce Categories?
Hi I'm currently going over old category pages for an ecommerce website, and I'm trying to work out the best way of handling old pages. I will be setting up 301s for the majority of pages, as these are 100% dead and gone. I'm struggling a bit with certain pages though, whereby the category is empty but there is always the possibility that our buyers will purchase these products again in the future (or they might not, there's no way to tell). I know that this isn't what a 302 is for, but I'm wondering which would be better in this case: to create a 302 redirect or to do a full 301 and if the products are repurchased at a later date to create a whole new url. Hope that makes sense. Thanks, Kate
Technical SEO | | Lisaangel0 -
Redirecting the .com of our site
Hey guys, A company I consult for has a different site for its users depending on the geography. Example: When a visitor goes to www.company.com if the user is from the EU, it gets redirected to http://eu.company.com If the user is from the US, it goes to http://us.company.com And so on. I have two questions: Does having a redirect on the .com will influence rankings on each specific sub-site? I suspect it will affect the .com since it will simply not get indexed but not sure if affects the sub domains. The content on this sub-sites are not different (I´m still trying to figure out why they are using the sub-domains). Will they get penalized for duplicate content? Thanks!
Technical SEO | | FDSConsulting0 -
How can I perform this 301 redirect?
I am working on a site for a colleague and have installed wordpress on their server in the wp directory, they want the root domain redirecting to the wp directory but everything i have tried seems to throw up errors. i need sample.co.uk to redirect to sample.co.uk/wp/ no matter which html file they are trying to access on the root of the sample.co.uk site help?
Technical SEO | | GrassRootsSEO0 -
Domain redirect seo
Hello, my domain www.pacomarca.com and when i start the new campaing i get this pronblem: We have detected that the domain www.pacomarca.com and the domain pacomarca.com both respond to web requests and do not redirect. Having two "twin" domains that both resolve forces them to battle for SERP positions, making your SEO efforts less effective. We suggest redirecting one, then entering the other here. my domain is in networksolutions.com. how can i resolve it? many thanks Gonzalo
Technical SEO | | Kuna0 -
Redirecting According to the occasion
We have loads of different departments that relate to specific occasions (e.g. Christmas, mothers day, father day etc), they all have good PR and inbound links etc and most of them rank well for the specific occasion. What I was thinking is that I could redirect all of the occasions that aren’t in use and send the traffic to the valid occasion live at the moment. So I would 301 all the Christmas pages to our valentine’s day main page. Then once valentines is gone I will redirect Christmas and valentines to the mothers day main page and so on. So this would be constantly switching as different occasions come along. Can you see any possible negative problems? Is there any chance that if I redirect Christmas for example, and then when Christmas comes we remove the redirect, could it harm the Christmas page?
Technical SEO | | steliosp210 -
Canonical tag, CNAME and 301 redirect
I have a website with a couple of domains pointing to one IP address. Let's say I have two domains www.example.com and www.example.ca I also see during my SEO analysis that the example.com and the www.example.com (same for the example.ca and the www.example.ca) are triggering server responses. How do I deal with this issue for best SEO. Canonical links? CNAME, or 301 redirects? thanks
Technical SEO | | casper4340 -
301 redirect issues
Hi all, I'm hoping someone will be able to help me with an extermley frustrating problem with 301 redirects in .htaccess. Basically I'm trying to redirect some old pages (from our old website) that stil rank to the new equivilent. For example - old url = www.domain.com/frames/news/company-news/news-reader.php?newsStoryID=395 New www.domain.com/news/article-title I've tried the simple redirect 301 /frames/news/company-news/news-reader.php?newsStoryID=395 http://www.domain.com/news/article-title But this doesnt work. I've also tried - RewriteEngine on
Technical SEO | | EclipseLegal
RewriteCond %{QUERY_STRING} ^newsStoryID=395$
RewriteRule ^/news-reader.php$ http://www.domain.com/news/article-title/? [L,R=301] Could anyone help? I've followed lots of tutorials that all match the above but it just doesn't work! The only other thing within the htaccess file is from wordpress for pretty permalinks - BEGIN WordPress <ifmodule mod_rewrite.c="">RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]</ifmodule> END WordPress Many thanks in advance!0 -
301 redirect on the root of the site
Due to some historic difficulties with our URL Rewriter, we are in the position of having the root of our site 301 redirected to another page. So the root of our site: http://www.propertylive.co.uk/ has a 301 redirect to: http://www.propertylive.co.uk/home.aspx We're aware that this isn't great and we're working to fix this completely, but what impact will this have on our SEO?
Technical SEO | | LianWard860