Forum Moderators: phranque
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]
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).
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
<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]
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 :)
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.
<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]
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?
[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
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
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
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