Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite - url to query string - help

         

Sudar

6:39 am on Oct 9, 2005 (gmt 0)

10+ Year Member



I want to set up the following url redirection in my website.

The url www.example.com/foo should be redirected to www.example.com/t.asp?s=foo

I was trying to do this with mod_rewite but couldn’t get it properly. More over I website is hosted with a hosting company and they do not allow me to change any configuration for Apache but I can have access to the .htaccess file. So could anyone kindly help me out.

[edited by: jdMorgan at 3:19 am (utc) on Oct. 10, 2005]
[edit reason] Example.com [/edit]

jd01

8:58 am on Oct 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Sudar,

Welcome to WebmasterWorld.

I am not sure I understand the question... are you wanting to REDIRECT people to the page so the URL shows as www.example.com/t.asp?s=foo in the browser (If you do are you sure? -- not generally the best course of action with SEs) OR are you wanting to REWRITE the information from t.asp?s=foo to the URL /foo so people see /foo in the browser, but get the information from t.asp?s=foo?

Please, let us know, and we will be able to point you in the direction of the answer...

I would also suggest making sure you can use mod_rewrite, with a simple test like this:

#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test\.html$ http://www.example.com/ [R,L]

Then type http://www.example.com/test.html in your browser -- If you are redirected to your homepage, mod_rewrite is on -- If not, you may not be able to use mod_rewrite.

(The first line may or may not be necessary. You can test both ways, by leaving or removing the #.)

Justin

[edited by: jdMorgan at 3:19 am (utc) on Oct. 10, 2005]
[edit reason] Example.com [/edit]

Sudar

2:32 pm on Oct 9, 2005 (gmt 0)

10+ Year Member



Hi Justin
Thanks for the wishes and the reply.

Actually I want to REWRITE the information from t.asp?s=foo to the URL /foo so people see /foo in the browser, but get the information from t.asp?s=foo

So that when they type www.example.com/foo they actually get the contents of www.mysite.com/t.asp?s=foo but the browser displays the url www.example.com/foo

And I tried your sample test code that you gave and made sure that mod_rewirte is working for me.

[edited by: jdMorgan at 3:20 am (utc) on Oct. 10, 2005]
[edit reason] Example.com [/edit]

jd01

5:48 pm on Oct 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first thing I suggest you do is visit the forum Charter [webmasterworld.com] and the Apache Forum Library [webmasterworld.com] -- they should give you an idea of what mod_rewrite can and cannot do, and it is always better to understand what you are working with, because the rules you will need *must* be exact, and any changes to you site or structure may require changes to your rules.

Second, you will need to make sure you link to the static version (/foo) of your pages. You cannot change the displayed URL with mod_rewrite, only the information served to a location (rewrite), or the location itself (redirect).

Then the rule you will use will depend on the structure of your site. If you only use the root level, you could use something like this:
RewriteRule ^([^.]+)/?$ /t.asp?variable=$1 [L]

If you have three levels of your site, you will need something more like this:
RewriteRule ^([^/]+/)?([^/]+/)?([^.]+)/?$ /t.asp?variable=$3 [L]

The basics of the regular expressions are:
([^.]+)='1 or more of any character that is not a .(dot)'
([^/]+/)?='1 or more of any character that is not a /' -- the trailing question mark means 0 or 1 of the preceding group, so the (grouping) is optional.

For more on the regular expressions, see the library and charter.

Let us know how it goes.

Hope this helps.

Justin

Added: added /? to the rules, so if the location is requested with a trailing / it will still be served the correct information -- can be removed.