Results 1 to 17 of 17
  1. #1
    Join Date
    Jun 2006
    Posts
    76

    htaccess redirect dynamic URL to dynamic URL

    Hi
    I have a page that show articles in my site , the url like this
    www.example.com/show-free.php?n=rest-bios&y=2007
    first i remove the the last variable 2007 because no need for it anymore, so the url become like this
    www.example.com/show-free.php?n=rest-bios
    and then i make rewrite by htaccess with this code
    RewriteRule show-free-n-(.*)\.html show-free.php?n=$1

    so the pages now show as
    www.example.com/show-free-n-rest-bios.html

    the problem is that old URLs are indexd by search engine, so if someone come to the site from search engine , he will access this page
    www.example.com/show-free.php?n=rest-bios&y=2007

    so i want to make redirect 301 from this page
    www.example.com/show-free.php?n=rest-bios&y=2007
    to
    www.example.com/show-free-n-rest-bios.html

    how to do that ?
    and its not only this page , so what i need is how to redirect dynamic URL to other dynamic URL

    thanks

  2. #2
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Try this:
    Code:
    RewriteEngine On
    RewriteCond %{QUERY_STRING} n=(.*)&y=2007
    RewriteRule show-free.php http://www.example.com/show-free-n-%1.html [R=301,L]
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  3. #3
    Join Date
    Mar 2005
    Posts
    31
    Quote Originally Posted by foobic View Post
    Try this:
    What if the query string parameters are in a different order? It won't work right. Actually the OP isn't interested in the year anyway so try:

    RewriteCond %{QUERY_STRING} n=(.*)&?
    RewriteRule show-free.php http://www.example.com/show-free-n-%1.html [R=301,L]

    You could also do it in your show-free.php script. Add a regex at the beginning of show-free.php that checks $_SERVER['REQUEST_URI'] to see if it contains the "old,ugly" url, and if it does, issue a 301 redirect through PHP to the "new,pretty,official" URI.
    Last edited by dmspilot; 12-30-2007 at 04:09 AM.

  4. #4
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Quote Originally Posted by dmspilot View Post
    Actually using mod_rewrite would be fine if you do it like this, because the OP isn't interested in the year:

    RewriteCond %{QUERY_STRING} n=(.*)&?
    RewriteRule show-free.php http://www.example.com/show-free-n-%1.html [R=301,L]
    Actually, that would cause a loop because if you read the OP he's also redirecting show-free-n-something.php back to show-free.php?n=something. The year in the query string is the only thing identifying an old link.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  5. #5
    Join Date
    Mar 2005
    Posts
    31
    Quote Originally Posted by foobic View Post
    Actually, that would cause a loop because if you read the OP he's also redirecting show-free-n-something.php back to show-free.php?n=something. The year in the query string is the only thing identifying an old link.
    Heh, you are right--it does loop, I apologize. That's not what I expected. I'll look into it more and see if I can get it to work as I imagined it would...
    Last edited by dmspilot; 12-30-2007 at 04:28 AM.

  6. #6
    Join Date
    Jun 2006
    Posts
    76

    its dont work

    hi and thanks guys for help

    i add this to my htaccess

    Code:
    RewriteEngine on
    RewriteCond %{QUERY_STRING} n=(.*)&?
    RewriteRule show-free.php http://www.lexample.com/show-free-n-%1.html [R=301,L]
    RewriteRule show-free-n-(.*)\.html show-free.php?n=$1
    still have problem when someone enter try to access the page by this url

    http://www.example.com/show-free.php?n=C-plus-plus-Variable-Scope&y=2007
    its rewrite and redirect it to

    http://www.lamp4arab.com/show-free-n-introduction&y=2007.html?n=introduction&y=2007
    i read much about that , but i did not know how to remove query from the redirected url

    so i try to change

    Code:
    RewriteCond %{QUERY_STRING} n=(.*)&?
    # to 
    RewriteCond %{QUERY_STRING} n=(.*)&y=(.*)
    thats give me the right page but the query string still in the end of the url
    http://www.lamp4arab.com/show-free-n-introduction.html?n=arabic-introduction&y=2007

    so should i used php code to remove the rest of query string and then redirect by used header("location:http://www.lamp4arab.com/show-free-n-introduction.html") ? and wont that be bad for SE ?
    Last edited by Badie; 12-30-2007 at 12:46 PM.

  7. #7
    Join Date
    Mar 2005
    Posts
    31
    Quote Originally Posted by Badie View Post
    thats give me the right page but the query string still in the end of the url
    Quote Originally Posted by mod_rewrite Documentation
    When you want to erase an existing query string, end the substitution string with just the question mark.
    So change this line:
    RewriteRule show-free.php http://www.lexample.com/show-free-n-%1.html [R=301,L]

    to this:
    RewriteRule show-free.php http://www.lexample.com/show-free-n-%1.html? [R=301,L]

  8. #8
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Quote Originally Posted by Badie View Post
    Code:
    RewriteCond %{QUERY_STRING} n=(.*)&y=(.*)
    thats give me the right page but the query string still in the end of the url
    http://www.lamp4arab.com/show-free-n...duction&y=2007
    Looks like it's doing a greedy match - strange, I thought it was non-greedy but it may be version-dependent.

    You could try this instead:
    HTML Code:
    RewriteCond %{QUERY_STRING} n=([a-zA-Z\-]*)&y=(.*)
    so should i used php code to remove the rest of query string and then redirect by used header("location:http://www.lamp4arab.com/show-free-n-introduction.html") ? and wont that be bad for SE ?
    If you do it using a PHP redirect you can still specify a 301, so for SEO it's exactly the same (just a little more resource-intensive).
    PHP Code:
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://www.example.com/show-free-$page.php"); 
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  9. #9
    Join Date
    Jun 2006
    Posts
    76
    Thanks
    now its work good
    i want to learn about re write and htaccess more, when i back apache doc, i did not understand anything , anyone knows good site that i can learn from it , something start from 0 ?

    thanks guys and happy new year

  10. #10
    Join Date
    Mar 2005
    Posts
    31
    You could try this:
    mod_rewrite cheat sheet

  11. #11
    Join Date
    Jun 2001
    Location
    Denver, CO
    Posts
    3,301
    I am having a bitch of time making something similar work. We have recently upgraded Kayako v2 to Kayako v3. As a result, the format of dynamic links have changed, as have the ID numbers for articles in our KB. Thus, I want to do redirects like the following:

    Old URL:http://www.helpdesk.handynetworks.co...ndetails&_i=48
    New URL: http://helpdesk.handynetworks.com/su...eid=44&nav=0,7

    Note that the article ID changed from 48 to 44 in this instance, and all others. I realize I will have to enter a bazillion rewrite rules to cover all of the articles, but at the very least, I need to get the major ones redirected (DMCA, AUP, TOS) as those old links are in who knows how many places ...

    So far, I have tried the following, just as a test:
    <code>
    rewritecond %{query_string} _a=knowledgebase&_j=questiondetails_i=48
    rewriteRule ^esupport/index.php$ http://www.google.com
    </code>

    Obviously, I am well aware this will redirect to Google. Unfortunately, the query_string condition isn't matching and I just can't figure out why.
    Last edited by Jay Suds; 01-17-2008 at 12:49 AM.
    Jay Sudowski // Handy Networks LLC // Co-Founder & CTO
    AS30475 - Level(3), HE, Telia, XO and Cogent. Noction optimized network.
    Offering Dedicated Server and Colocation Hosting from our SSAE 16 SOC 2, Type 2 Certified Data Center.
    Current specials here. Check them out.

  12. #12
    Join Date
    Jun 2001
    Location
    Denver, CO
    Posts
    3,301
    And just to add, this is very strange, this rule works:

    rewriteCond %{query_string} _a=knowledgebase&_j=questiondetails_i=48
    rewriteRule ^page.html$ http://www.google.com? [R,L]
    r

    http://www.helpdesk.handynetworks.co...ondetails_i=48
    this will redirect to google.com

    but

    rewritecond %{QUERY_STRING} _a=knowledgebase&_j=questiondetails_i=48
    rewriteRule ^esupport/index.php$ http://www.google.com

    http://www.helpdesk.handynetworks.co...ndetails&_i=48

    doesn't. Is the PHP engine screwing with the query string???
    Jay Sudowski // Handy Networks LLC // Co-Founder & CTO
    AS30475 - Level(3), HE, Telia, XO and Cogent. Noction optimized network.
    Offering Dedicated Server and Colocation Hosting from our SSAE 16 SOC 2, Type 2 Certified Data Center.
    Current specials here. Check them out.

  13. #13
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    I'd guess it's because you're using esupport/file in your RewriteRule. Does the esupport directory exist? Any conflicting rules in .htaccess there?

    If you're replacing the esupport directory anyway I'd just put your new rules in there as RewriteRule index.php whereeveritsredirectingto.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  14. #14
    Join Date
    Jun 2001
    Location
    Denver, CO
    Posts
    3,301
    That esupport directory doesn't exist any more. Strangle, this rule does work ...

    rewritecond %{QUERY_STRING} ^_a=k(.*)_i=48$
    rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=44&nav=0,7 [R=301,L]

    I don't get it, but I'm glad it's working.
    Jay Sudowski // Handy Networks LLC // Co-Founder & CTO
    AS30475 - Level(3), HE, Telia, XO and Cogent. Noction optimized network.
    Offering Dedicated Server and Colocation Hosting from our SSAE 16 SOC 2, Type 2 Certified Data Center.
    Current specials here. Check them out.

  15. #15
    Join Date
    Jun 2001
    Location
    Denver, CO
    Posts
    3,301
    argh. That rule actually seems to be capturing anything with a query string. so for example, now I have

    rewritecond %{QUERY_STRING} ^_a=k(.*)_i=48$
    rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=44&nav=0,7 [R=301,L]

    rewritecond %{QUERY_STRING} ^_a=k(.*)_i=46$
    rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=42&nav=0,7 [R=301,L]

    And it doesn't matter what i= equals, everything ends up going to the first URL
    Jay Sudowski // Handy Networks LLC // Co-Founder & CTO
    AS30475 - Level(3), HE, Telia, XO and Cogent. Noction optimized network.
    Offering Dedicated Server and Colocation Hosting from our SSAE 16 SOC 2, Type 2 Certified Data Center.
    Current specials here. Check them out.

  16. #16
    Join Date
    Mar 2005
    Posts
    31
    Quote Originally Posted by Jay Suds View Post
    argh. That rule actually seems to be capturing anything with a query string. so for example, now I have

    rewritecond %{QUERY_STRING} ^_a=k(.*)_i=48$
    rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=44&nav=0,7 [R=301,L]

    rewritecond %{QUERY_STRING} ^_a=k(.*)_i=46$
    rewriteRule ^esupport/index.php$ /supportsuite/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=42&nav=0,7 [R=301,L]

    And it doesn't matter what i= equals, everything ends up going to the first URL
    That's not possible. Considering you set a 301 Moved Permanently redirect code your browser probably (should have) cached the redirect the first time you tried it, it's not going to make a new request if you try to visit that link again, so Apache has no opportunity to use the updated RewriteRules. Try clearing your cache or use the "Live HTTP headers" extension for Firefox.

  17. #17
    Join Date
    Jun 2001
    Location
    Denver, CO
    Posts
    3,301
    You're right. It was a caching issue.
    Jay Sudowski // Handy Networks LLC // Co-Founder & CTO
    AS30475 - Level(3), HE, Telia, XO and Cogent. Noction optimized network.
    Offering Dedicated Server and Colocation Hosting from our SSAE 16 SOC 2, Type 2 Certified Data Center.
    Current specials here. Check them out.

Posting Permissions

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