Forum Moderators: phranque

Message Too Old, No Replies

rewrite help request

it should be no problem for you guys

         

operafan

12:06 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



Sorry to disturb you guys, but here's my request

how to
convert abc.com/page.htm?catid=2 or id number can range from 2 to 200 ( 3 digits )
to abc.com/id2.htm , id2.htm respectively.

I find rewritemap complicated, so if i had to code all the id numbers I don't mind to represent the static page output, but we can use regular expression to solve the numberical id problems right. Please help. thanks

ukgimp

12:19 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here is my rough stab

rewriteRule abc\.com\/catid=([0-9]{1, 3})\.htm http:\/\/www\.abc\.com\/page\.htm\/?catid=$1 [L]

The bit in curly braces is where you change the numbers.

Regex tutorial
[etext.lib.virginia.edu...]

[edited by: ukgimp at 12:36 pm (utc) on Jan. 8, 2004]

Birdman

12:29 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

Mod_rewrite will do what you need with one rewrite rule. I'm assuming you are doing this for search engine friendly URLs. If you changed all your URLs to point to /id101.htm, etc., the rule below will rewrite them into the original URL so your script will work perfectly and the URLs will appear static.

Hope it helps.

RewriteEngine on
rewriteRule ^id(.*)\.htm$ /page.htm?catid=$1 [L]

operafan

1:12 pm on Jan 8, 2004 (gmt 0)

10+ Year Member



Sorry guys but it wont work? I've tested it live on my own box of win/apache with htaccess enabled.

tried both of you guys recommendations but cant do

here's my code
RewriteEngine On
RewriteRule ^\CatId=([0-9])\.htm$ /page\.htm/?CatId=$1 [L]

no error what so ever.. I tried other .htaccess file for redirecting of user & it works perfectly..?

jdMorgan

9:20 pm on Jan 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To clarify: You want to convert a query-string based URL to a static-looking URL, and then feed that to a script?

If so, you'll need to use RewriteCond %{QUERY_STRING} <pattern> to access the query-string; mod_rewrite does not consider the query string in RewriteRules.

It is more usual to have your script output friendly (static) URLs to browsers and search engines, and then convert those static URLs back into queries to your script. Mod-rewrite acts after a request is received, but before your script is called. mod_rewrite has no effect after your script is called.

Therefore, it's important to know which way you actually want the conversion to work.

Jim

operafan

1:25 am on Jan 9, 2004 (gmt 0)

10+ Year Member



Ok, yah man...now I get the gist of it for those query strings stuff.. my first time for query strings...I prefer to use rewrite rule to fix it rather than script..will try your suggestions on the rewritecond's that's why I thought i was missing something. thanks

operafan

9:59 am on Jan 9, 2004 (gmt 0)

10+ Year Member



And so I've tried out a few rewriteconds - query strings & request uri but brings no conversion
.
here's some of my test:
For URI method
RewriteEngine On
RewriteCond %{Request_URI} "\.htm?CategoryId=3$"
RewriteRule ^page\.htm?categoryid=(3)$ page$1\.htm [L]

while Querystring method which

RewriteCond %{query_string} ^$
RewriteRule ^page\.htm?categoryid=(3)$

Tried some of php scripts online to to convert from static to php but to no avail too.

<?
function processURI() {
global $REQUEST_URI;

$array = explode("'(?<!/)page.htm\?CategoryId=3'",$REQUEST_URI);
$num = count($array);

$url_array = array(subpage3.htm);

for ($i = 1 ; $i < $num ; $i++)
$url_array["arg".$i] = $array[$i];

return $url_array;

function displayContent($array)
section = $array["arg3"];
$cat = $array["arg4"];

$content = "content/"
. $section
. "_"
. $cat
. ".htm";

if (!file_exists($content))
content = "error.php";
include($content);
} else {
include($content);
}
}

?>

<?
$myarray = processURI();
?>

So any hints? clues, pls pretty pls...

jdMorgan

6:43 pm on Jan 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



operafan,
how to convert example.com/page.htm?catid=2 or id number can range from 2 to 200 (3 digits) to example.com/id2.htm , id2.htm respectively.

Internal redirects:


RewriteCond %{QUERY_STRING} ^catid=([0-9]{1,3})$
RewriteRule ^page\.htm$ /id%1.htm [L]

or converting the other way:

RewriteRule ^id([0-9]{1,3})\.htm$ /page.htm?catid=$1 [L]

External redirects:


RewriteCond %{QUERY_STRING} ^catid=([0-9]{1,3})$
RewriteRule ^page\.htm$ http://example.com/id%1.htm [R=301,L]

or converting the other way:

RewriteRule ^id([0-9]{1,3})\.htm$ http://www.example.com/page.htm?catid=$1 [R=301,L]

The above code allows id numbers of 0 to 999. If constraining the id numbers to range between 2 and 200 (inclusive) and requiring the non-leading-zero format is important, then replace all occurrances of
"([0-9]{1,3})"
- with -
"([2-9]¦[1-9][0-9]¦1[0-9]{2}¦200)".

Change all broken pipe characters "¦" to solid pipe characters (usually SHIFT \ on keyboard) before use.

The above code is intended for use in an .htaccess context. If you wish to use it in httpd.conf, then you will have to add a leading slash to the pattern in each RewriteRule, e.g.

 RewriteRule [b]^/p[/b]age\.htm$ /id%1.htm [L] 

Jim

operafan

4:05 am on Jan 10, 2004 (gmt 0)

10+ Year Member



Thank you so much Jim, it worked fine, but the output still includes the?catid=3 behind ..
I'm figuring out how to remove the string behind.

jdMorgan

4:17 am on Jan 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I forgot that part. Add a question mark to the substitution string (it will 'clear' the query string, but it will not show in the redirect):

Internal:


RewriteCond %{QUERY_STRING} ^catid=([0-9]{1,3})$
RewriteRule ^page\.htm$ /id%1.ht[b]m?[/b] [L]

External:

RewriteCond %{QUERY_STRING} ^catid=([0-9]{1,3})$
RewriteRule ^page\.htm$ http://example.com/id%1.ht[b]m?[/b] [R=301,L]

Again, all the notes in my post above about httpd.conf, pipe characters, etc., apply.
Jim

operafan

4:23 am on Jan 10, 2004 (gmt 0)

10+ Year Member



Works great, thank you so much Jim. Yes thank you also for the notes if I plan to do it on the httpd.conf than I'll have to add a slash - read that lately in the apache docs..
Thank you again, thank you.

operafan

8:29 am on Jan 10, 2004 (gmt 0)

10+ Year Member



Would like to clarify if I would to use the L flag, it would be a silent redirect & I see that it reflects on the log 200 while using R is 304 so which would be better for the bots? I guess I should stick with L then.. ?

jdMorgan

8:54 am on Jan 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



operafan,

[L] means that no more rewrite rules need to be processed for this request - it is the usual default.
[R], [R=301], or [R=302] is an external redirect, and you must specify a full URL, like "http://www.example.com/file.pl"
If there is no [R], then you must specify a local path only, like "/file.pl"

Use the flags [R] and [L] exactly as shown above for maximum happiness. ;)

If you want the new URL to show in your browser, then use an external redirect, [R=301,L]. If not, then use an internal rewrite only [L].

A server response code of 304 means that the requested page contents have not been changed since last requested, and so it was not loaded from your server, it was loaded from your browser or ISP cache -- flush your cache before testing!

Jim