Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts

    htaccess 301 redirect not working

    Hi guys i know this is not php but i cant see where this should go on this forum.

    I also know that you guys know htaccess as i see you guys using it some times.

    I know a little bit of it and normally i do not need help with htaccess but i cant seem to get the 301 redirect to work.

    I am wanting to change an ugly old file which is:

    blog-article.php?id=14

    to

    blog-post/logo-in-5-minutes-photoshop-tutorial/1/

    And so on for a few of them.

    The redirect is so that the pr rank goes to its new url as these are pr3-4 so do not want to lose these ranks.

    Here is the code, could you please tell me what i am doing wrong.

    Code:
    RewriteRule ^blog-article.php?id=14 http://www.domain.co.uk/blog-post/logo-in-5-minutes-photoshop-tutorial/1/ [R=301,L]
    Thanks

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,345
    Thanks
    60
    Thanked 527 Times in 514 Posts
    Blog Entries
    4
    Quote Originally Posted by devinmaking View Post
    Hi guys i know this is not php but i cant see where this should go on this forum.
    Erm.. how about here: Apache configuration

    How you missed that is beyond me
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Thanks,

    I dont know why i could not see that lol.

    Do i start a new thread or weight for mod to move it?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Yep, moving to apache configurations.

    This is backwards. Unless you have a specific index file to work with? Typically its the other way around, you would map a more textual representation to that of using the id's or names. I don't recommend going the other way around.
    The url will show what the user types in. You cannot force that to change, only where it goes. So if they type in ?id=14, that is where they go and what they will see. You can write a replacer in PHP to have it provide the links in an expanded format, but I wouldn't recommend hard coding that since that now requires that mod_rewrite be in use. Declare an environment variable in the <IfModule mod_rewrite.c> block, and alter the links as desired in PHP.

  • #5
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by Fou-Lu View Post
    Yep, moving to apache configurations.

    This is backwards. Unless you have a specific index file to work with? Typically its the other way around, you would map a more textual representation to that of using the id's or names. I don't recommend going the other way around.
    The url will show what the user types in. You cannot force that to change, only where it goes. So if they type in ?id=14, that is where they go and what they will see. You can write a replacer in PHP to have it provide the links in an expanded format, but I wouldn't recommend hard coding that since that now requires that mod_rewrite be in use. Declare an environment variable in the <IfModule mod_rewrite.c> block, and alter the links as desired in PHP.
    Ive done all the php work

    This is for search engines and other sites that are linking to www.domain.co.uk/blog-article.php?id=14 to redirect to its new page which is search engine friendly.

  • #6
    New Coder
    Join Date
    Nov 2011
    Posts
    88
    Thanks
    4
    Thanked 26 Times in 26 Posts
    I agree, this is back to front. Typically you'd have a link like:

    http://www.mydomain.null/logo-in-5-m...op-tutorial/1/

    and mod_rewrite would match that, and internally redirect it to:

    www.domain.co.uk/blog-article.php?id=14

    You probably could make your rule to do it the other way around, but I don't see much point if you are trying to tidy things up and make them clearer, as you are actually munging things and making them less clear.

    It's also worth mentioning that if the search engine is already listing:

    www.domain.co.uk/blog-article.php?id=14

    in its index, then I'd say the URL was pretty much 'search engine friendly' already :-)

  • #7
    New Coder
    Join Date
    Nov 2011
    Posts
    88
    Thanks
    4
    Thanked 26 Times in 26 Posts
    Just adding to that, if you *really* want to do it (and for the sake of Google bringing people here):

    The reason your version fails is because of the ? question mark ? You cannot use the question mark in the match as it signals a querystrings which is caught as separate data.

    This basic example will do what you want for the specific URL quoted, but it can probably be better refined or optimised.
    Code:
    RewriteCond %{REQUEST_URI} ^/blog-article.php$
    RewriteCond %{QUERY_STRING} ^id=14$ 
    RewriteRule ^(.*)$ http://www.google.co.uk [R=301,L]
    Hope that helps.

  • #8
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by leslie.jones View Post
    Just adding to that, if you *really* want to do it (and for the sake of Google bringing people here):

    The reason your version fails is because of the ? question mark ? You cannot use the question mark in the match as it signals a querystrings which is caught as separate data.

    This basic example will do what you want for the specific URL quoted, but it can probably be better refined or optimised.
    Code:
    RewriteCond %{REQUEST_URI} ^/blog-article.php$
    RewriteCond %{QUERY_STRING} ^id=14$ 
    RewriteRule ^(.*)$ http://www.google.co.uk [R=301,L]
    Hope that helps.
    I was told by someone from seomoz that to keep the pr4 rank for that page plus to keep google happy by the links still going to the relevant pages to 301 direct the old url to the new one.

    Can someone please tell me why this is backwards?

    Its only applying to what google are asking for is it not?

    Thanks

  • #9
    New Coder
    Join Date
    Nov 2011
    Posts
    88
    Thanks
    4
    Thanked 26 Times in 26 Posts
    I'm no expert on Google metrics - so I'm not qualified to answer, but in a logical context I would look at it like this:

    LINK GOES DIRECTLY TO RELEVANT CONTENT WITHOUT INTERVENTION
    LINK = GOOD, KEEP IN INDEX

    LINK GOES INDIRECTLY TO RELEVANT CONTENT VIA A REDIRECT
    LINK = RUBBISH, NOT POINTING TO RIGHT PLACE, DROP/MODIFY

    Effectively, and I stress as a LOGICAL problem, I don't want a link to something being redirected - I want to go direct to the resource, and I would penalise for a redirect (which means not only the link is effectively pointing to the wrong resource, but also resulting in more traffic to achieve the required result).

    To square that, a redirect is probably useful if you move a page and want it re-indexed. However there will probably come a point where if you redirect the same resource for two long there may be a pay-off or penalisation metric.

    I must stress, however, I know very little about SEO, I know about Apache. As far as SEO is concerned I've always been lucky enough to get sites listed and well placed in search engines by using clean, relevant content with properly formatted pages and targeting specific demographics for the business concerned, but I know others who have more general sites in swamped sectors have an uphill battle.

  • #10
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by leslie.jones View Post
    I'm no expert on Google metrics - so I'm not qualified to answer, but in a logical context I would look at it like this:

    LINK GOES DIRECTLY TO RELEVANT CONTENT WITHOUT INTERVENTION
    LINK = GOOD, KEEP IN INDEX

    LINK GOES INDIRECTLY TO RELEVANT CONTENT VIA A REDIRECT
    LINK = RUBBISH, NOT POINTING TO RIGHT PLACE, DROP/MODIFY

    Effectively, and I stress as a LOGICAL problem, I don't want a link to something being redirected - I want to go direct to the resource, and I would penalise for a redirect (which means not only the link is effectively pointing to the wrong resource, but also resulting in more traffic to achieve the required result).

    To square that, a redirect is probably useful if you move a page and want it re-indexed. However there will probably come a point where if you redirect the same resource for two long there may be a pay-off or penalisation metric.

    I must stress, however, I know very little about SEO, I know about Apache. As far as SEO is concerned I've always been lucky enough to get sites listed and well placed in search engines by using clean, relevant content with properly formatted pages and targeting specific demographics for the business concerned, but I know others who have more general sites in swamped sectors have an uphill battle.
    As the page of blog-article?id=14 was moved to a seo friendly url is that not a reason for a redirect?

    If i dont redirect then google will take away the pr4 next time round, i am confused now lol...

  • #11
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by leslie.jones View Post
    Just adding to that, if you *really* want to do it (and for the sake of Google bringing people here):

    The reason your version fails is because of the ? question mark ? You cannot use the question mark in the match as it signals a querystrings which is caught as separate data.

    This basic example will do what you want for the specific URL quoted, but it can probably be better refined or optimised.
    Code:
    RewriteCond %{REQUEST_URI} ^/blog-article.php$
    RewriteCond %{QUERY_STRING} ^id=14$ 
    RewriteRule ^(.*)$ http://www.google.co.uk [R=301,L]
    Hope that helps.
    The only problem with this is you can only use this once.

    i have 8 urls.

    id=6 etc once, you have queried the blog once it does not allow you to do the same for others.

  • #12
    New Coder
    Join Date
    Nov 2011
    Posts
    88
    Thanks
    4
    Thanked 26 Times in 26 Posts
    Normally (which is a bit of a generalisation as there is no such thing tbh).....

    You would have some clear and defined structure, and be working the other way round, that is:

    Code:
    LINK: mysite.com/tutorials/1 --->REDIRECT ---> mysite.com/show.php?id=1
    LINK: mysite.com/tutorials/2 --->REDIRECT ---> mysite.com/show.php?id=2
    LINK: mysite.com/tutorials/3 --->REDIRECT --->mysite.com/show.php?id=3
    With this, your rewrite code can grab the '/1, /2, /3' and use that data in the redirect response - so you would have one rule to deal with all cases, plus a safety trap that redirects any non-existent files/pages should some user request something like mysite.com/tutorials/9999999999.

    But you are working back to front because you are trying to transcribe:
    Code:
    LINK: blog-article.php?id=14 --->REDIRECT ---> /blog-post/logo-in-5-minutes-photoshop-tutorial/1/
    From a code perspective there is no structure for mod_rewrite to work with, and you have no choice but to use an individual rule for id=14 for it to redirect to: /blog-post/logo-in-5-minutes-photoshop-tutorial/1/. If you only have 8 links it is not the end of the world, just write 8 rules. It's not optimal, but it should work without obvious issue.

    MOD_REWRITE is not always the answer. Sometimes it is better to have a scripted handler that grabs the querystring, looks it up in a local array, table or database, and redirects based upon that. In your case, where your links are already making use of a querystring it may be a better option to put a simple php redirector together.

    As for page ranking et al, it is not an area I can honestly say I am confident to give good advice in. I think in terms of logic, mostly, but this may be flawed and not match the metric used by search engines.

    id=6 etc once, you have queried the blog once it does not allow you to do the same for others.
    Once you arrive at the target page (by redirection or directly) your links on that page should be fixed to point directly to the correct place and need no redirection, surely?
    Last edited by leslie.jones; 01-05-2012 at 01:34 PM.

  • #13
    Regular Coder
    Join Date
    Oct 2011
    Posts
    237
    Thanks
    11
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by leslie.jones View Post
    Normally (which is a bit of a generalisation as there is no such thing tbh).....

    You would have some clear and defined structure, and be working the other way round, that is:

    Code:
    LINK: mysite.com/tutorials/1 --->REDIRECT ---> mysite.com/show.php?id=1
    LINK: mysite.com/tutorials/2 --->REDIRECT ---> mysite.com/show.php?id=2
    LINK: mysite.com/tutorials/3 --->REDIRECT --->mysite.com/show.php?id=3
    With this, your rewrite code can grab the '/1, /2, /3' and use that data in the redirect response - so you would have one rule to deal with all cases, plus a safety trap that redirects any non-existent files/pages should some user request something like mysite.com/tutorials/9999999999.

    But you are working back to front because you are trying to transcribe:
    Code:
    LINK: blog-article.php?id=14 --->REDIRECT ---> /blog-post/logo-in-5-minutes-photoshop-tutorial/1/
    From a code perspective there is no structure for mod_rewrite to work with, and you have no choice but to use an individual rule for id=14 for it to redirect to: /blog-post/logo-in-5-minutes-photoshop-tutorial/1/. If you only have 8 links it is not the end of the world, just write 8 rules. It's not optimal, but it should work without obvious issue.

    MOD_REWRITE is not always the answer. Sometimes it is better to have a scripted handler that grabs the querystring, looks it up in a local array, table or database, and redirects based upon that. In your case, where your links are already making use of a querystring it may be a better option to put a simple php redirector together.

    As for page ranking et al, it is not an area I can honestly say I am confident to give good advice in. I think in terms of logic, mostly, but this may be flawed and not match the metric used by search engines.


    Once you arrive at the target page (by redirection or directly) your links on that page should be fixed to point directly to the correct place and need no redirection, surely?
    I had an old website that was really outdated.

    There was no seo within the pages, the urls were all variables etc and not pretty.

    So i completely re built the website.

    Now the website has a different structure and completely different urls.

    For instance my old contact page was say-hello.php

    The new one is now, /contact-us/

    The reason for the 301 is so that all old pages point to the same content which has now moved in url.

    Ive spoken to people on seoforum.ord and seomoz.com and they have all stated that a 301 is a must if i want to keep page authority and rank for each and every page, plus all links from external websites still point to a relevent page.

    That is why this is needed.

    The query code which was given above made it so it added the id=14 on the end of the redirect which is causing a problem, which means the query can only be input once because on the next one it shows a page 404 instead.

    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •