Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-18-2011, 08:18 PM   PM User | #1
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 158
Thanks: 4
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
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?
keith1995 is offline   Reply With Quote
Old 03-21-2011, 06:42 AM   PM User | #2
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 158
Thanks: 4
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
Anyone got a tip/code to accomplish this?
keith1995 is offline   Reply With Quote
Old 03-21-2011, 07:13 AM   PM User | #3
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Try
Code:
RewriteEngine On

RewriteRule ^attorneydetails\.php(.*)$ practiceareas\.php$1 [L]
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-21-2011, 04:39 PM   PM User | #4
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 158
Thanks: 4
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
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?
keith1995 is offline   Reply With Quote
Old 03-21-2011, 04:55 PM   PM User | #5
abduraooft
Supreme Master coder!

 
abduraooft's Avatar
 
Join Date: Mar 2007
Location: N/A
Posts: 14,680
Thanks: 158
Thanked 2,182 Times in 2,169 Posts
abduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really niceabduraooft is just really nice
Quote:
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]
?
__________________
Quote:
The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)
abduraooft is offline   Reply With Quote
Old 03-21-2011, 07:18 PM   PM User | #6
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
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
Krupski is offline   Reply With Quote
Old 03-21-2011, 08:38 PM   PM User | #7
keith1995
Regular Coder

 
Join Date: Jul 2004
Posts: 158
Thanks: 4
Thanked 0 Times in 0 Posts
keith1995 is an unknown quantity at this point
Krupski,

I don't need to redirect to a whole new domain name, rather just redirect one dynamic page to another dynamic page.
keith1995 is offline   Reply With Quote
Old 03-22-2011, 12:38 AM   PM User | #8
Krupski
Regular Coder

 
Krupski's Avatar
 
Join Date: Dec 2010
Location: United States of America
Posts: 502
Thanks: 39
Thanked 47 Times in 46 Posts
Krupski is on a distinguished road
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
Krupski is offline   Reply With Quote
Old 04-11-2011, 04:28 PM   PM User | #9
domenicfz
New to the CF scene

 
Join Date: Aug 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
domenicfz is an unknown quantity at this point
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
domenicfz is offline   Reply With Quote
Old 04-11-2011, 10:49 PM   PM User | #10
munkeyboy
New Coder

 
Join Date: Apr 2011
Posts: 36
Thanks: 0
Thanked 3 Times in 3 Posts
munkeyboy is on a distinguished road
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
munkeyboy is offline   Reply With Quote
Old 09-23-2011, 02:05 PM   PM User | #11
classics40
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
classics40 is an unknown quantity at this point
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.
classics40 is offline   Reply With Quote
Old 09-23-2011, 03:54 PM   PM User | #12
classics40
New to the CF scene

 
Join Date: Sep 2011
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
classics40 is an unknown quantity at this point
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
classics40 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:51 AM.


Advertisement
Log in to turn off these ads.