Forum Moderators: phranque
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
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]
RewriteRule ^courseid/ http://www.example.com/courses/?s=st&c=courseid [QSA,R=301,L]
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
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]
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]
$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]
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]
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]
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]