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 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts

    301 redirect with PHP variables in URL

    We need to redirect a URL to a new URL but we can't get it to work and I believe it is due to the inclusion of variables in the URL.

    For example, we need to redirect http://www.domain.com/attorneydetail...tion=view&id=8 to http://www.domain.com/practiceareas....tion=view&id=2

    How would we do this in a .htaccess file?

  • #2
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Anyone got a tip/code to accomplish this?

  • #3
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Try
    Code:
    RewriteEngine On
    
    RewriteRule ^attorneydetails\.php(.*)$ practiceareas\.php$1 [L]
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #4
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    abduraooft,

    This is definitely a step in the right direction, thank you for that.

    This code rewrites all of the attorneydetails.php to practiceareas.php pages, we just need it done for one page, not the full group.

    Also, the URL remains the original URL, can that be updated in the URL bar to reflect the actual URL it is forwarded to?

  • #5
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,801
    Thanks
    160
    Thanked 2,216 Times in 2,203 Posts
    Blog Entries
    1
    Also, the URL remains the original URL, can that be updated in the URL bar to reflect the actual URL it is forwarded to?
    Code:
    RewriteRule ^attorneydetails\.php(.*)$ /practiceareas\.php$1 [R]
    ?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #6
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by keith1995 View Post
    We need to redirect a URL to a new URL but we can't get it to work and I believe it is due to the inclusion of variables in the URL.

    For example, we need to redirect http://www.domain.com/attorneydetail...tion=view&id=8 to http://www.domain.com/practiceareas....tion=view&id=2

    How would we do this in a .htaccess file?
    You're doing it the hard way. Simply put an index.php file in the current root of your webserver and make it point to your new site directory by placing this inside:

    Code:
    <?php
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://new.url.of.your.website.com");
        exit();
    ?>
    No messing around with .htaccess or rewrite engines required.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #7
    Regular Coder
    Join Date
    Jul 2004
    Posts
    158
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Krupski,

    I don't need to redirect to a whole new domain name, rather just redirect one dynamic page to another dynamic page.

  • #8
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts
    Quote Originally Posted by keith1995 View Post
    Krupski,

    I don't need to redirect to a whole new domain name, rather just redirect one dynamic page to another dynamic page.
    It will still work. That's all I do is redirect one page to another. Here's my exact index.php file:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <title>Redirecting you...</title>
        </head>
        <body style="background-color:#99ccff;padding:1.0em;">
            <?php
                header("HTTP/1.1 301 Moved Permanently");
                header("Location: http://three-dog.homelinux.com/phpbb/index.php");
                exit();
            ?>
            <div style="background-color:#fff7f0;width:50%;text-align:center;padding:3.0em;border:2px solid #336699;">
                <a href="http://three-dog.homelinux.com/phpbb/index.php">
                    <span style="font-weight:bold;font-size:16px;">Please click this link if you are not automatically redirected</span>
                </a>
            </div>
        </body>
    </html>
    See? All I do is redirect "index.php" from my web root to "webroot/phpbb/index.php".

    Works fine.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #9
    New to the CF scene
    Join Date
    Aug 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What about URL Variables?

    I have a landing page that I need to hit and then have that landing page redirect the user to a different page. I am stuck on keeping the URL variables in tact when the redirect happens.

    htttp://www.domain1.com/test.php?variable1=test&variable2=test2

    once that page is hit, I need it to redirect to lets say domain2 but keep the variables:

    htttp://www.domain2.com/test.php?variable1=test&variable2=test2

  • #10
    New Coder
    Join Date
    Apr 2011
    Posts
    36
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by keith1995 View Post
    We need to redirect a URL to a new URL but we can't get it to work and I believe it is due to the inclusion of variables in the URL.

    For example, we need to redirect http://www.domain.com/attorneydetail...tion=view&id=8 to http://www.domain.com/practiceareas....tion=view&id=2

    How would we do this in a .htaccess file?
    At the very top of attorneydetails.php enter:
    PHP Code:
    if( $_GET['id'] == ){
    header"HTTP/1.1 301 Moved Permanently" );
    header"Location: http://www.domain.com/practiceareas.php?$_SERVER['QUERY_STRING']" );
    exit();

    Thanks to Krupski since I copied your code
    Last edited by munkeyboy; 04-13-2011 at 07:46 AM. Reason: give thanks

  • #11
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    RewriteCond %{QUERY_STRING} ^id=108
    RewriteCond %{QUERY_STRING} lang=fr$
    RewriteRule index.php http://www.yoursite.com/nameforseo/fr/nameforseo/nameforseo? [L,R=301]
    so here i want to redirect for example : index.php?id=108&page=content&lang=fr
    *note that i filter variable id and language in two different rewrite conditions.

    Then i redirect to any page i want. Also note the ? at the end of my rewrite rule, the ? stops the automatic transmission of previous Get variables to the new redirect url.

  • #12
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by classics40 View Post
    Code:
    RewriteCond %{QUERY_STRING} ^id=108
    RewriteCond %{QUERY_STRING} lang=fr$
    RewriteRule index.php http://www.yoursite.com/nameforseo/fr/nameforseo/nameforseo? [L,R=301]
    so here i want to redirect for example : index.php?id=108&page=content&lang=fr
    *note that i filter variable id and language in two different rewrite conditions.

    Then i redirect to any page i want. Also note the ? at the end of my rewrite rule, the ? stops the automatic transmission of previous Get variables to the new redirect url.
    if for example you have index.php?var1=232&id=108&var2=blla&lang=fr
    change the
    [CODE]RewriteCond %{QUERY_STRING} ^id=108
    RewriteCond %{QUERY_STRING} lang=fr$

    for
    [CODE]RewriteCond %{QUERY_STRING} &?id=108
    RewriteCond %{QUERY_STRING} lang=fr$

    it will look for it where ever the var is situated in the url


  •  

    Posting Permissions

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