I am looking to create an SEO friendly URL after a filter has been submitted in an html form.
After playing around for a while I have found a way, however I was wondering what otehr people think of it as I'm new to development.
I have added some rewrite rules in the .htaccess file to make the urls more friendly. Examples below:
Original URL:
site-nane/list.php?brand=brand1&min-price=0&max-price=2000
URL after rewrite:
site-name/section/brand1/0-200
Currently I have the form that submits the information to a separate php page which collects the variables and creates a new url from it which then redirects with a 301. Example of php below:
$min = $_GET[‘min-price’];
$max = $_GET[‘max-price’];
$brand = $_GET[‘brand’] ;
header ('HTTP/1.1 301 Moved Permanently');
header('Location: http://site-name/section/' . $brand . '/'. $min.'-'.$max );
exit();
As you can see it collects the info and takes you back to the page and declares the previous page has permanently moved.
Questions:
Although this maybe quite primitive, will this still be ok to use without causing too much trouble?
Will google hate me for creating so many 301’s
Just noticed the code header("Location: /foo.php",TRUE,301); would it be best to use this or no difference?
Yes, I see no issues with your solution. Even if malicious user input was given it would just redirect to a non-existing page.
I don't think so. You already use the right code 301 instead of the default 302 which might cause some trouble / did create some havoc with regard to Google, stolen PR and SEO
Using header("Location:...", true, 301); is advisable. This way php could automatically make decisions based on the environment. E.g. if using an HTTP/1.0 connection php could send the 301 code with HTTP/1.0 instead of your fixed HTTP/1.1 in your solution. But still, either way is fine.
But one question: why don't you link directly to your nice URL? mod_rewrite which you are using would then already take care of assigning the parameters given with the URL to variables that you could access via $_GET as usual.
The way you've done things is a permutation of the post redirect get (PRG) pattern. In your case, it's Get redirect get.
What PRG normally means is:
User POSTs the form to a controller which does whatever you want, and build the desired url
Script REDIRECTs use to the desired url
User GETs the url, sees the result.
Generally speaking, it's a good pattern to follow, it allows you the control to - e.g. - remove default values from the resultant search url.
Regarding your specific questions
If it works, it's fine
Google won't be submitting forms, so it won't be finding the pre-processed urls to get 301-redirected unless those urls exist somewhere else already. However, you need to be linking to "http://site-name/section/brand/min-max" for any search engine to know the pages exist.
Either is fine, one line of code is easier to type though
Search engines do not submit forms.
Thus, no form action have to be friendly. (And,as another consequence - no, google won't hate you).
There is just no point in doing additional redirect instead of displaying form results.
Related
I've been reading about redirection, and how it can affect (or not if done properly) SEO.
I'm changing my website's content platform from Drupal to a PHP custom made code.
In my current site I have two links that point to the same link like this:
.../node/123
.../my-node-title
Mainly because Drupal allows you to create a custom-made links, so every article has a default one (node/123) and the custom-made one (/my-node-title).
My question is about what to do in order to prevent losing any SEO that each link may have.
In the new website all articles are structured like this: content.php?id=123
I've stored in the database the custom-made link of every article.
Instead of doing a 301 redirect I'm redirecting all links that do not exist to be redirected to redirect.php page to process the request. There I take the string from the link, look for it in the database and redirect the user.
The process is like this:
in .htaccess file:
RewriteRule ^.*$ ./redirect.php
In redirect.php:
I grab the $_SERVER['REQUEST_URI'] and using explode() I get the last part of the link (ie. my-node-title), look for it in the database and grab the ID of the article (ie. 123) and save it in a $link variable.
Then I use header() function and do the redirect: header('Location: '.$link);
So, people still click on .../my-node-title but when the article loads at the navigation bar appears /content.php?id=123
I would like to know your comments about this solution. I know that with SEO there are not fixed rules, or certainty in anything, but I would like to know if what am I doing is acceptable. Thanks!
Your SEO strategy should not only focus on discoverability of your pages, but also take proper UX into account. Having a user follow /some-link/, and then landing on /index.php?page_id=123 may disorient them.
As for saving your ranking, a 302 redirect (which is what the 'Location' header does in PHP), will not affect PageRank, according to Google. I have no information on how it might adversely affect other ranking signals. You would probably do good to specify a canonical URL for all distinct links that point to the same resource.
Also, be aware that your algorithm won't work, if query parameters are present. You might also want to look at properly handling optional trailing slashes.
Ideally, in my opinion, you would want to provide consistent URLs to the outside world, without any need for redirection. Your URL handling would then internally resolve them to their respective resources, serving the canonical URL on every page load.
I am developing a website crawler using golang. When i tried to crawl some websites, I am getting weird results. Root Url of some website returns script tag as shown below.
<script>window.location="index.php";</script>
And it redirects to index.php page. Why people are using this approach to redirect user to index page. Any security vulnerability with this approach? And also, how can i handle this situation in crawler?
Well, if you really want to hide the page by redirecting the user to another page, then you obviously cannot use this method, because anyone can turn javascript off and see the page, thus this can be a security risk. However, if you just simply want to redirect for some reason, this is fine.
As for you crawler, what you can do is search the source code with regex for redirections like that, but it can be very challenging to cover all cases.
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.
I'm new to mod_rewrite and need to do something for my client.
Suppose I have the www.mydomain.com/products.php?prod_id=32.
This product has a section (clothes) and a name (shirt). These names and sections are unique.
In a SEO-Friendly Url, it should be www.mydomain.com/products/clothes/shirt/.
I know I can create
RewriteRule ^products/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ products.php?section=$1&name=$2
I can do that, and it works. But I want people who enter www.mydomain.com/products.php?prod_id=32 to be redirected to www.mydomain.com/products/clothes/shirt/ (changed in the browser itself). How can I do that without inserting the id in my url? Is it possible to call a "pre-processing" php file in my .htaccess, and recreate "products.php?section=$1&name=$2"?
Anyone has a good link with really detailed explanation of mod_rewrite?
Thanks!
You may have a bigger problem than mod_rewrite can handle gracefully. You can also use PHP to return a redirect to the browser. That way you can access your database to figure out that product_id 32 is /clothes/shirts/
I see no other option than doing it inside PHP.
You can add something to the top of your products.php page that checks the URL ($_SERVER['REQUEST_URI']) to see if it contains products.php - If it does, redirect the person. You'll need to query your database to find out the product category and the name before redirecting though.
Remember to set the Moved Permanently header to improve SEO further :)
This data might be passed by the browser via the "Referer" header field. You could progress that url and look at the get arguments. If I remember right, this isn't supported on all browsers.
at the top of products.php (or any page you want to redirect) you could put a function call
redirectToSeoUrl();
Then in one of your include files write a redirectToSeoUrl function that gets the new url and redirects. Make sure to put the code before anything is output to the browser.
Apache? Try an .htaccess redirect. No mod_rewrite needed.
Example:
Redirect 301 /oldpage.html http://www.example.com/newpage.html
I currently have a community site that I run that is made up of 15 or so php pages. It is not currently very dynamic, only using php for includes/templates.
Currently no content is generated via a query string. The pages do not follow a standard naming convention and there are many inbound links.
I am looking to expand the site (and start using query strings to build pages) but before I do this I want to change over to use pretty url’s. With this in mind I have a few questions though.
1) Is it best to point all requests to a url rewriting page catch any request to .php pages first and pass them through to keep existing links then a giant case statement for each of the 15 pages finally the rewrite the url's of new pages as these will follow a set format?
2) How can I prevent duplicates in google after I have updated my sitemap.xml or will it remove the old pages?
Thanks
1) I'd redirect using apache's URL rewrite, and leave that static. It'll avoid the mess of having those 15 files you already have in your site.
I hope I have not misunderstood your question and this helps.
2) Edit robots.txt in the root of your website to tell google (and most others) what it should index, and what it shouldn't:
Google's info on the subject: http://www.google.com/support/webmasters/bin/answer.py?hl=en&answer=40360
Wikipedia: http://en.wikipedia.org/wiki/Robots_exclusion_standard
More info: http://www.robotstxt.org/
You should use a 301 permanent redirect from the old pages to the new URLs. This will redirect users that follow links from the old to the new and Google will pass the PageRank you have accumulated on the old pages to the new one. You might also look at using Googles new canonical tag on the old pages to ensure they transfer authority to the new page.
http://googlewebmastercentral.blogspot.com/2009/02/specify-your-canonical.html
In .htaccess, you want a bunch of
redirect 301 /old/old.htm http://www.you.com/new.htm
Regardless how its implemented, make sure any redirects use HTTP status 301, not the default (in may systems) of 302.
302 = Moved
301 = Moved permanently.
Using 301 helps google replace the old with the new URL, and should help pagerank etc carry over as well.