Forum Moderators: phranque

Message Too Old, No Replies

Replacing character using mod_rewrite

character inside variable

         

Deep

8:16 am on Sep 24, 2004 (gmt 0)

10+ Year Member



Hi,
I checked post about replacing character in URL but i want to replace character inside the variable i.e.

[mysite.com...]

to [mysite.com...]

basically i can't figure out how to do that..

anyone can help me here?

Thanks
Deep

jdMorgan

1:26 pm on Sep 24, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Deep,

Welcome to WebmasterWorld!

The following code demonstrates replacing "¦" in the query string with "-" in the URL, but may not be exactly what you need, because you need to decide whether you want an external redirect or an internal rewrite, whether you want to put the code in httpd.conf or in .htaccess, and whether you actually want to redirect from a dynamic URL to a static URL (it is more usual to rewrite from a static URL to a dynamic one, but that's another subject).


RewriteCond %{QUERY_STRING} cat=([0-9]+)\¦([0-9]+)\¦([0-9]+)&name=([a-z]+)
RewriteRule index\.php$ http://www.example.com/%1-%2-%3-%4.html? [R=301,L]

This code requires that three numeric cat sub-parameters separated by "¦" be present in the query string, and that the name parameter to be alphabetic. It also requires the query parameters to be in the order cat, name. The code may change completely if other requirements must be met.

Jim

Deep

3:11 pm on Sep 24, 2004 (gmt 0)

10+ Year Member



Thanks a lot Jim,
I will try it tmrw on our server :-)

btw i am gonna add it in httpd.conf becasue effect i heard that it is much faster than adding .htaccess file..

and one more thing..

in your reply you wrote

RewriteRule index\.php$ [example.com...] [R=301,L]

but for other rewrite rules i have been writing like this..

RewriteRule NewURL $ OldURL

and can you also tell me what will be the effect of R=301 here? 301 is for permenant redirect right?

so does that mean if google or any bot visits the url with query string then it will be redirect to new one and they will take it (redirect) as..url is permenantly moved to new one?

please correct me if i am wrong..

Regards
Deep

Deep

5:41 am on Sep 25, 2004 (gmt 0)

10+ Year Member



UPDATE:
Hi,
I used the code but it didnt work for me :(

i had used this simple code before which didnt have the whole url in the rewrite rule..

RewriteRule /test/(.*)/(.*)/$ /folder1/folder2/index.php?cat=$1&name=$2

so output for above code will be

[mysite.com...]

so i applied same login with your code also..i.e changed it to foll

RewriteCond %{QUERY_STRING} cat=([0-9]+)\¦([0-9]+)\¦([0-9]+)&name=([a-z]+) 
RewriteRule /folder1/folder2/index\.php$ /test/%1-%2-%3-%4.html? [R=301,L]

this also didnt work so i did like this...

RewriteCond %{QUERY_STRING} cat=([0-9]+)\¦([0-9]+)\¦([0-9]+)&name=([a-z]+) 
RewriteRule /test/%1-%2-%3-%4\.html$ /folder1/folder2/index\.php [R=301,L]

but again no luck..

let me know if i did anything wrong in above code..i am pretty new to this mod_rewrite thing but it seems to be real interesting to me..

thanks for the help
Deep

jdMorgan

2:35 pm on Sep 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but for other rewrite rules i have been writing like this..
>
> RewriteRule NewURL $ OldURL

I would strongly suggest that you read the mod_rewrite documentation [webmasterworld.com], because that is not how mod_rewrite works.

The documented syntax is:
RewriteRule OldURLpattern Substitution

Where substitution is the new URL.

Perhaps it is just your terminology or expectations that are off-track here; Mod_rewrite rewrites URLs received by your server from client browsers and spiders to new URLs before any content is served or any scripts are run on your server.

Therefore, if you wish to use static links on your pages and convert those to dynamic links for use by your script inside the server, your pages must contain static links. Mod_rewrite cannot affect the way that pages are displayed, it can only affect the way that requests for URLs are handled when they are received.

If you pages contain links in the form:

[mysite.com...]

then those are the links that browsers will use. If you want those URLs to be translated into dynamic links in the form:

[mysite.com...]

then that is the exact opposite of what you asked for in your first message.

It is also a simpler case to code:


ReriteRule /([^-]+)-([^-]+)-([^-]+)-([^.]+)\.htm$ /index.php?cat=$1¦$2¦$3&name=$4 [L]

It should be noted that this code rewrites from the URL 0-23-45-test.htm requested by the browser to the index.php?cat=0¦23¦45&nmae=test URL needed to run your script.

If you are not committed to using "¦" in your URLs, I would strongly advise you not to. Using this character may cause problems later, since "¦" is a reserved character in URLs and a "command" character in regular expressions and mod_rewrite. A hyhen is actually a better choice.

Jim

Deep

12:27 pm on Sep 28, 2004 (gmt 0)

10+ Year Member



Hi Jim,
I think i confused you..

i will explain again by following simple example..

my current url is

[mysite.com...]

I want it to be

[mysite.com...]

i did whatever you have mentioned in the first reply but that did not work for me...

it will be graet if you can help me out here :-)

and yes about syntax of Rewriteurl i said NewURL $ OldURL because it worked for me till now..

for example..

my current url was [abc.com...]

so to make it as [abc.com...]

i wrote rule like this:

RewriteRule id(.*)\.htm$ id.php?id=$1

i.e. NewURL$ OldURL

and this worked for me...

anyways..i think i shall not bug you with another doubt now :D

Thank you again for great help.
Regards
Deep

jdMorgan

1:48 pm on Sep 28, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> i.e. NewURL$ OldURL

The confusion is apparently due to the context of "old" and "new." You are thinking in terms of the old URL that you used to use versus the new URL that you want to use now. I am thinking in terms of mod_rewrite's function -- the URL being requested (old or input URL) versus the URL needed to locate the requested resource (new or output URL).

The syntax of mod_rewrite in these terms is


RewriteRule <requested or input URL> <actual or output URL>

I have provided examples of how to rewrite both static and dynamic links above. Without knowing exactly what you are trying to do, that's the best I can do. Please take those as examples to use in learning how to modify your code to fit your needs.

Jim

Deep

2:25 pm on Sep 28, 2004 (gmt 0)

10+ Year Member



No probs i will figure it out..

thanks for the help.
Deep