homepage Welcome to WebmasterWorld Guest from 54.198.42.105
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
redirect php file with query string to new site intact
Keep query string and php file format wduring redirection
jimmbo




msg:4597642
 12:52 pm on Jul 30, 2013 (gmt 0)

I have a very large database-driven site (mysite.com) and a copy site (mysite.co.uk). I need to redirect thousands of pages cached by search engines from the copy site to the main site because I am shutting the copy site down.

I need to redirect using .htaccess (mod-rewrite is enabled)

Redirect www.mysite.co.uk/phpfiles/myfile.php?someID=123456 to

www.mysite.com/phpfiles/myfile.php?someID=123456

Any help gratefully received.

 

Dideved




msg:4597686
 3:11 pm on Jul 30, 2013 (gmt 0)

The redirect directive is what you want.

https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect

jimmbo




msg:4597712
 4:58 pm on Jul 30, 2013 (gmt 0)

I have looked at this but am still somewhat confused. My .htaccess file uses...

RewriteEngine On

For SPECIFIC files or folders WITHOUT a query string that need to be changed on redirect to a different file name WITH a query string...

"RedirectMatch 301 /specificFileName(.*) http://www.example.com/phpfiles/myfile.php?someID=1234"

and for all basic .html files...

"Redirect 301 blah blah"

All these work fine.

Can you give me a code example based on the initial qestion above - I have looked at many redirect examples but none do what I want - which is to redirect the hundreds of cached search engine files from the .co.uk site that have query strings to the new .com site using exactly the same query string format.

[edited by: phranque at 6:06 pm (utc) on Jul 30, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]

g1smd




msg:4597748
 5:47 pm on Jul 30, 2013 (gmt 0)

Redirect all requests that are for host names that are not exactly www.example.com

RewriteRule On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Install the above code on the old hosting in the root htaccess file.

At some point soon, add the exact same code to the htaccess file of the new site.

Next, alter the DNS for the .co.uk hostname so that to points to the new hosting, the same hosting used for the .com hostname.

You can then dispense with the old hosting. Both domains will point at the new hosting and the htaccess rule there will force everyone to use the .com version of every URL.

jimmbo




msg:4597956
 8:31 am on Jul 31, 2013 (gmt 0)

g1smd - Thanks for the response.

I am sure this will work but I need to keep the old .co.uk domain available as a separate site as we intend to develop this to focus on local matters rather than general international information. This is why I need the regex code to redirect the query strings.

I understand I will need to use RewriteCond but what has me stumped is the correct regex for RewriteCond and RewriteRule. Can you oblige please?

RewriteRule On
RewriteCond .............
RewriteRule ............. [R=301,L]

phranque




msg:4597967
 9:35 am on Jul 31, 2013 (gmt 0)

the TestString of your RewriteCond directive should use the %{QUERY_STRING} server variable.

it could be as simple as this:
RewriteRule On
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


the above code will redirect any request that contains any query string to the www.example.com hostname while retaining the originally requested path and query string.


"RedirectMatch 301 /specificFileName(.*) http://www.example.com/phpfiles/myfile.php?someID=1234"
and for all basic .html files...
"Redirect 301 blah blah"
All these work fine.


Redirect(Match) are mod_alias directives and don't play well with mod_rewrite directives.
you will have to recode all these as RewriteRule directives.

g1smd




msg:4597987
 12:06 pm on Jul 31, 2013 (gmt 0)

If the .co.uk hostname is re-used for a new site and this new site uses internal rewrites, then you need to be more specific still.

If you test only QUERY_STRING this will result in requests for the new .co.uk site that are internally rewritten, being inadvertantly redirected to the .com site, exposing the rewritten internal path at the same time.


Try this:

RewriteRule On
RewriteCond %{THE_REQUEST} \?
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


This rule will, however, completely rule out query string use on the new site that is developed.

If the new .co.uk site will use URL rewriting, the
(.*) pattern should be further refined so that it matches only requests for the old site URLs, perhaps ^(index\.php) or somesuch.

The new .co.uk site would then use a different PHP script file name to avoid any clash.

jimmbo




msg:4598072
 3:27 pm on Jul 31, 2013 (gmt 0)

These two suggestions look much closer to the answer I need. Many thanks....

However some of the query string requests made on the old .co.uk site used .asp files whilst the .com site is entirely php. We need to modify these URL requests as shown below:-

Here are three examples of the redirection that is needed to ensure complete redirection occurs - and by being so specific it still allows us to use query strings on the old co.uk site when it is redeveloped.

example 1:

/phpfiles/tree.asp?nodeID=24 - a co.uk request that needs to be changed to

/phpfiles/tree.php?nodeID=24 on the .com site (note the file extension is changed from asp to php)

example 2:

/phpfiles/branch.php?nodeID=1001 - a co.uk request that needs to be changed to

/phpfiles/branch.php?nodeID=1001 - on the .com site (here the requests appear identical)

finally example 3 - great if we can do this as well:

/anotherfolder/somefile.asp?id=1234 - a co.uk request that needs to be changed to

/aDifferentFolder/somenewfile.php?param=5678 - here the URL, file extension and query string parameters are all changed.

Sorry its so complicated!

lucy24




msg:4598231
 9:41 pm on Jul 31, 2013 (gmt 0)

Example 1 is utterly straightforward. Since you're redirecting, it doesn't matter whether the hostname is the same or different; you'll be using it in the target either way.

Can we assume for the sake of discussion that none of your directory names contain literal periods? The ones in the hostname don't count. Then you get

RewriteRule ^([^.]+)\.asp http://www.example.com/$1.php [R=301,L]

By default, any query string is carried over unchanged. So you don't need to say anything about it at all.

You can collapse examples 1 and 2 into one by replacing \.asp with \.(?:asp|php). Here it does matter what the target hostname is: You can't redirect back to the same domain, or you'll get an infinite loop until the browser steps in. Different domains will be fine.

Example 3: Any specific redirects have to come before the generic redirects of 1 and 2. Here the pattern will be

RewriteCond %{QUERY_STRING} ^id=1234$
RewriteRule ^anotherfolder/somefile\.asp http://www.example.co.uk/aDifferentFolder/somenewfile.php?param=5678 [R=301,L]

If the target of a redirect includes a query string, it will replace any previous query unless you specifically tell it to keep the old one too. You can put a query string in a target but you can't put it in the body of the pattern; it has to be expressed as a separate Condition.


Have you got a bunch of pre-existing rules using mod_alias (Redirect by that name) that you need to change to mod_rewrite? If there are loads of them, you can open your htaccess in a text editor that does Regular Expressions and run a couple of quick global replaces.

g1smd




msg:4598241
 10:38 pm on Jul 31, 2013 (gmt 0)

Lucy is on the right track, but I would again add a RewriteCond looking at THE_REQUEST so that only external requests for these patterns get redirected.

You are then restricted to not use these patterns in the URLs for any new .co.uk site that is developed but are free to internally use .asp files if you want, just as long as the matching URLs are extensionless or at least don't fit any of your "redirect to .com" patterns.

lucy24




msg:4598258
 11:16 pm on Jul 31, 2013 (gmt 0)

Oh, wait, overdue question. Do the two sites live on the same server? Do requests for the two sites pass through the same htaccess? It's definitely less complicated if there is no overlap. This will depend on the hosting setup.

g1smd




msg:4598363
 10:05 am on Aug 1, 2013 (gmt 0)

That's one issue, but the over-riding issue is that the OP wants to redirect certain .co.uk requests to the .com site and then re-use the .co.uk for a new site. Extreme care will need to be taken to ensure that external requests for new .co.uk URLs and internal requests for new .co.uk stuff do not ever match any of the "redirect to .com" rules. So, all of the "redirect to .com" rules should only match external URL requests for old .co.uk URLs. They should never match internal requests of any kind, so they never accidentally redirect new .co.uk requests and expose and project the new internal paths onto .com URLs.

The old .co.uk site used ASP. If the new .co.uk site uses both PHP and extensionless URLs, the risk of a problem developing is much more remote.

jimmbo




msg:4598369
 10:31 am on Aug 1, 2013 (gmt 0)

Many thanks to you both. Well done.

I have now decided to use the following format - which works fine.

RewriteRule ^(.*tree)\.(?:asp|php) http://www.example.com/$1.php [R=301,L]

This allows me to specify the exact file name precursors that need redirection. We are thinking how best to use/avoid using example 3 as it will involve quite a bit of work.

g1smd




msg:4598443
 3:48 pm on Aug 1, 2013 (gmt 0)

Don't forget to add the RewriteCond checking THE_REQUEST so that only external URL requests are redirected.

jimmbo




msg:4598692
 8:33 am on Aug 2, 2013 (gmt 0)

Thanks for the reminder g1smd. Just a quick enquiry re the syntax. Is it sufficient to use RewriteCond just once - or is it needed above every different redirect? ...


RewriteRule On
RewriteCond %{THE_REQUEST} \?
RewriteRule ^(.*tree)\.(?:asp|php) http://www.example.com/$1.php [R=301,L]
RewriteRule ^(.*branch)\.(?:asp|php) http://www.example.com/$1.php [R=301,L]
RewriteRule ^(.*twig)\.(?:asp|php) http://www.example.com/$1.php [R=301,L]
.
'
'
etc

lucy24




msg:4598728
 11:08 am on Aug 2, 2013 (gmt 0)

Each RewriteCond applies ONLY to the immediately following RewriteRule.

g1smd




msg:4598729
 11:09 am on Aug 2, 2013 (gmt 0)

A RewriteCond applies only to the next RewriteRule listed after the Condition(s).

A RewriteRule can have multiple Conditions that apply to it, but they will apply only to that one rule.

Add a blank line after each RewriteRule so that the code is more readable.

Add a code comment to each rule so that you know what it is supposed to do. You'll thank yourself when you revisit the code several years in the future.

You can simplify by combining your three rules into one by using
(tree|branch|twig) and adjusting the $n numbers in the rule target.

Must type faster. 60 seconds would have done it.

jimmbo




msg:4598824
 3:39 pm on Aug 2, 2013 (gmt 0)

Thanks g1smd - all done abd working!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved