Forum Moderators: phranque

Message Too Old, No Replies

simple htaccess php redirect

Trying to pass php variable in redirect

         

mysteray

4:20 pm on Jun 26, 2007 (gmt 0)

10+ Year Member



Hi. I'm trying to write a series of redirects for my website because we are changing the directory structure. I've tried looking for examples and tried to figure out how to write the htaccess code, but it is all very confusing to me. I'm sure what I'm trying to do is simple.

I currently have a directory structure like: www.example.com/courseid/?h=1
where the h= part can be any number

I'm changing that to: www.example.com/courses/?s=st&c=courseid&h=1
where the c= part is the old directory and h= remains the same. I'm adding an s= parameter as well, which is an unknown variable and i believe will have to be hard coded for each courseid

I have it working now where if i go to www.example.com/courseid it redirects to www.example.com/?s=st&c=courseid, but i'm having trouble with the h= part of it. here's what i have right now.

Redirect 301 /courseid http://www.example.com/courses/?s=ST&c=courseid

any help is appreciated.
-adam

jdMorgan

9:59 pm on Jun 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm afraid this problem is unsolvable, except on a URL-by-URL basis.

The reason is that neither mod_alias not mod_rewrite has any way to know what values to put into your new variables, and so cannot be used to 'generate' them in any automated fashion.

In order to 'preserve' query strings through a redirect, you'll need to use mod_rewrite. Here is an example:


RewriteCond %{QUERY_STRING} ^(h=[0-9]+)$
RewriteRule ^courseid/ http://www.example.com/courses/?s=st&c=courseid&%1 [R=301,L]

If query strings attached to /courseid/ URLs *always" contain only h=<number> and *never* contain anything else, and if you do not care what order the name/value pairs occur in, then you can simply that and use:

RewriteRule ^courseid/ http://www.example.com/courses/?s=st&c=courseid [QSA,R=301,L]

Before using that, be very careful that all of your /courseid/ URLs meet those requirements -- not a single exception...

Allow me to interject that you're about to put a lot of effort into making URLs that search engines won't like. You might want to consider creating and using static-looking 'search-engine-friendly' URLs instead. More info here [webmasterworld.com].

Jim

mysteray

5:12 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



jim. i entered your suggestion into my .htaccess file, but it doesn't seem to be working. here's what i have:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^(h=[0-9]+)$
RewriteRule ^rosedale/ http://www.testserver.example.com/courses/?s=ks&c=rosedale&%1 [R=301,L]

if i navigate to http://testserver.example.com/rosedale/ it doesn't redirect. if i remove the directory "rosedale", i get an error message "The requested URL /rosedale was not found on this server."

am I missing something? is it because i'm using a subdomain?

[edited by: jdMorgan at 7:27 pm (utc) on June 27, 2007]
[edit reason] example.com [/edit]

mysteray

5:18 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



sorry, just did some more testing. it works if i navigate to http://testserver.example.com/rosedale/?h=1. however, it's possible to go to just http://testserver.example.com/rosedale/, which is the equivalent of http://testserver.example.com/rosedale/?h=0

is there a way to satisfy both scenarios?

thanks. -adam

[edited by: jdMorgan at 7:27 pm (utc) on June 27, 2007]
[edit reason] example.com [/edit]

Milamber

5:26 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



%1 should be $1

jdMorgan

7:35 pm on Jun 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> %1 should be $1

$1 refers to a parenthesized sub-pattern in the RewriteRule pattern.
%1 refers to a parenthesized sub-pattern in the last-matched RewriteCond pattern.

So here, %1 is correct.

To solve the "h=0" problem, something like:


RewriteEngine on
RewriteCond %{QUERY_STRING} ^(h=[0-9]+)$ [OR]
RewriteCond %{QUERY_STRING}>>h=0 ^>>(.+)$
RewriteRule ^rosedale/ http://www.testserver.example.com/courses/?s=ks&c=rosedale&%1 [R=301,L]

Note that the ">>" string is arbitrary and means nothing; It is used only as a unique demarcation between the two parts of the RewriteCond string that is to be compared to the pattern.

In this case, if the query_string is blank, then we force it to "h=0".

Jim

[edited by: jdMorgan at 7:36 pm (utc) on June 27, 2007]

mysteray

9:20 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



i actually went with this solution:

RewriteCond %{QUERY_STRING} ^(h=[0-9]+)$
RewriteRule ^rosedale/ [testserver.example.com...] [R=301,L]
RewriteRule ^rosedale/ [testserver.example.com...] [R=301,L]

this seems to be working. if the URL was typed without the h=, i didn't want to add it.

i have about 50 of these sets. will that bog down the load times of my pages at all? also, do i want to put a RewriteEngine off at the end of it all?

thanks for all the help. -adam

[edited by: jdMorgan at 9:36 pm (utc) on June 27, 2007]
[edit reason] example.com [/edit]

jdMorgan

9:35 pm on Jun 27, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> i have about 50 of these sets. will that bog down the load times of my pages at all?

Yes, it will consume CPU resources. Whether that could be described as "bogging down" your server or not depends on your server's CPU, memory, and disk speed, what kind of bandwidth it has available, and how many requests per second you're getting.

If the URLs share common features, take advantage of the available pattern-matching and back-references to 'compress' the redirects into fewer rules.

> also, do i want to put a RewriteEngine off at the end of it all?

No, RewriteEngine off is only really needed when you want to disable a block of rules during testing. It may save a bit of parsing time to use it if your rules are followed by a large block of non-mod_rewrite directives, but it's not required at all.

[added] Please don't post real domains here; Since you've posted your testserver's domain several times already, be sure that it is firewalled and/or has a robots.txt file or other access controls in place to deny access. The 'bots will likely be there momentariy... Please review our charter and TOS. :) [/added]

Jim

[edited by: jdMorgan at 9:41 pm (utc) on June 27, 2007]

mysteray

9:47 pm on Jun 27, 2007 (gmt 0)

10+ Year Member



yeah, sorry about that. i got copy/paste happy. the testserver subdomain is password protected and will be removed once our site update is completed. there is also a robots.txt file in place on the root domain. thanks for making the edits.