Forum Moderators: phranque

Message Too Old, No Replies

Mod_rewrite question

getting rid of ugly urls

         

chrisuk

11:55 am on Jan 6, 2004 (gmt 0)

10+ Year Member



Hello

I have a syntax query about mod_rewrite. I have it setup correctly on our server but am trying to determine the correct syntax to use with my test script.

What I can't work out is how to get rid of undesirable characters like the? and = that result in ugly urls. In my test script I want to translate the urls exactly like below:

[example.com...]
into
[example.com...]

Obviously "keyword" should represent the search term being used, so if someone is searching for say, "cars" the url dynamically becomes:

[somesite.com...]

Do any of you mod_rewrite experts know the correct syntax to create a rule that would do this please?

Many thanks indeed.

Chris

[edited by: jatar_k at 5:14 pm (utc) on Jan. 6, 2004]
[edit reason] no personal urls thanks [/edit]

brotherhood of LAN

12:18 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hey Chris, you might have better luck in the Apache forum with this question, but here's a stab at your answer....not that I'm a guru! (I didn't test this either).

RewriteRule ^terms/search\.cgi\?Terms=(.+)$ /terms/$1.html [L]

The (parenthesis) grabs anything after "Terms=" and places what it finds where you want it in new URI. If you have another variable you may want to change (.+) to ([^&]+).

If case isn't important, change the [L] flag to [L,NC], which will match the URI regardless of letter case.

Hopefully that's right ;) As said, Apache forum is maybe a better place to ask (mods may move the thread anyway).

chrisuk

12:28 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Thanks and will try your syntax and see if it works, apologies if this post is better suited in apache.

The only thing that is case sensitive I think is "Terms" as "terms" won't work.

Hopefully this will do what I want.

Chris

chrisuk

1:52 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Hmm

I am not sure that worked.
I was hoping to be able to type in

[example.com...]
instead of
[example.com...]

but I get a 404 error
Could the syntax be slightly off do you think?

Thanks

Chris

chrisuk

1:58 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



It might help more if I post my actual directives for the actual test script which include your suggestion:

<Directory "/home/example.com/htdocs/terms">
Options +FollowSymlinks
RewriteEngine on
RewriteBase /terms/
RewriteRule ^terms/meta_results\.cgi\?Terms=(.+)$ /terms/$1.html [L,NC]
</Directory>

The desired end result being that

/terms/meta_results.cgi?Terms=keyword
becomes
/terms/keyword.html

keyword is of course dynamic and adjusts to whatever search term is queried.

Any use?

[edited by: jdMorgan at 6:47 pm (utc) on Jan. 9, 2004]
[edit reason] Removed URL specifics [/edit]

brotherhood of LAN

2:23 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



>Could the syntax be slightly off do you think?

Very possible, I hashed through a couple of examples and the? mark seems to be the problem, I thought escaping it with backslash would be OK.

Would be willing to chip in a suggestion but I'd have to test it myself, more willing to let a real guru chip in with the answer :)

ScottM

2:49 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about:

RewriteEngine on
RewriteBase /terms/
RewriteRule ^meta_results\.cgi\?Terms=(.+)$ $1.html [L,NC]

Haven't tried it, but it may work. Also, might there be a need for an escape \ after Terms, but before the =?

As in:

RewriteEngine on
RewriteBase /terms/
RewriteRule ^meta_results\.cgi\?Terms\=(.+)$ $1.html [L,NC]

Just some ideas.

chrisuk

5:53 pm on Jan 6, 2004 (gmt 0)

10+ Year Member



Thanks guys
I got it going but your advice really helped.

Ta

Chris

ScottM

6:11 pm on Jan 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Could you post the code that worked? I'm sure others as well as myself would like to see it and learn from it.

:>)

chrisuk

12:18 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



Yes I will do that when I access the server next, I will post the code here.

Thanks

chrisuk

2:35 pm on Jan 9, 2004 (gmt 0)

10+ Year Member



This is what did it

<Directory "/home/example.com/htdocs/terms/">
Options +FollowSymlinks
RewriteEngine on
RewriteBase /terms/
RewriteRule ^(.*)\.html$ /cgi-bin/rewrite/meta_results.cgi?Terms=$1 [L]
</Directory>

All works okay now

[edited by: jdMorgan at 6:48 pm (utc) on Jan. 9, 2004]
[edit reason] Removed URL specifics [/edit]

chrisuk

11:28 am on Jan 13, 2004 (gmt 0)

10+ Year Member



Does anyone now know if it is possible to add additional mod_rewrite rules that would do this.

to turn
[example.com...]

into

subdomain and url string:
keyword.example.com/terms/keyword.html

Therefore if you were to access keyword.example.com/terms/keyword.html in the browser the test script would grab the content from [example.com...] and return it as keyword.example.com/terms/keyword.html

Because "keyword" is dynamic (it could be anything) what changes, if any might you have to make to your server to make the names resolve?

Does this make any sense and can it be done?

Istvan

6:19 pm on Jan 13, 2004 (gmt 0)

10+ Year Member



I see that the discussion goes about mod_rewrite as well. At the moment I have the following urls:

[somewidgetdomain.com...]

now I would like to rewrite this to:
[somewidgetdomain.com...]

As I understood it when the apache server rewrites it, the searchengine spiders see redwidgets.htm and are able to index it, right?

Also how do I rewrite the above, please help.

Istvan

chrisuk

8:40 am on Jan 14, 2004 (gmt 0)

10+ Year Member



Can anyone help with my query?

jdMorgan

9:10 am on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



chrisuk,

We'll be happy to help you debug your code or to discuss it. But our charter [webmasterworld.com] specifies that we can't write it for you. There are simply too many seekers and not enough oracles here to do otherwise. We can discuss your code or generalities of coding, or we can recommend doing a site search for the keywords that describe your problem, in this case "query string rewrite [google.com]." We prefer to "teach you to fish," rather than to "hand you one fish." Your understanding is appreciated.

The key to your issue is that query strings are not part of the URL-path pattern that a RewriteRule can "look at." In order to test and manipulate query strings, you must use RewriteCond. RewriteCond teststring parts matching parenthesized subgroups in the condpattern may be back-referenced in the substitution of the following RewriteRule using %1, %2, etc.

Look up RewriteCond [httpd.apache.org], and use the server variable %{QUERY_STRING} to test your query string or to create a back-reference to it, or to parts of it.

Jim

chrisuk

10:33 am on Jan 14, 2004 (gmt 0)

10+ Year Member



Yeah I understand the need to find out how something works for yourself but this is obviously quite tricky, even my host didn't know how to do it.

Below is some code I have been working with

<Directory "/home/mysite.co.uk/htdocs/terms/">
Options +FollowSymlinks
RewriteEngine on
RewriteBase /terms/
RewriteRule ^(.*)\.html$ /cgi-bin/rewrite/meta_results.cgi?Terms=$1
RewriteCond %{REQUEST_URI}!^/index\.php
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST} ^(.+)\.mysite\.co.uk
RewriteRule .* /meta_results.cgi?s=%1 [L]
</directory>

If I could get pointed in the right direction then I could go from there.

Thanks

jdMorgan

6:12 pm on Jan 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



chrisuk,

How does "s=" query part enter into it? You didn't describe it as part of the final URI, so it is confusing here.

With the requested URI you describe, and the rules you posted above (neither of which shows any external redirect), the browser URI will not be changed by mod_rewrite. However, if your script modifies the response headers to include a 301 0r 302 redirect, then the browser may show the "ugly" query-based URI. What I'm saying is that if your internal query is showing at the browser, it is because of a script action, and not because of anything that your mod_rewrite code is doing.

Jim