homepage Welcome to WebmasterWorld Guest from 54.161.175.231
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / WebmasterWorld / Webmaster General
Forum Library, Charter, Moderators: phranque & physics

Webmaster General Forum

    
Apache Rewrite Module - who's the man?
TOTAL NEWBIE - want to rewrite an url text but don't know how
Robbatse




msg:392023
 2:52 pm on Oct 1, 2002 (gmt 0)

hi!

please help me I am totally new to this aspect of my apache webserver:

I want to rewrite the url

[url...] into
[url...]

what should I write after rewriterule?
the english dokumentations are too complex for me to understand
(I'm from germany and too stupid to understand *G*)

thx for everyone who helps...

 

hpche




msg:392024
 3:01 pm on Oct 1, 2002 (gmt 0)

I think you could do it like this:

RewriteRule ^directory/(.*)/$ directory/search.php?search=$1

Robbatse




msg:392025
 3:14 pm on Oct 1, 2002 (gmt 0)

where do I have to place it?

http.conf:

<virtualHost>
?
</...>

or where else?

andreasfriedrich




msg:392026
 3:26 pm on Oct 1, 2002 (gmt 0)

There was an article by Ralf S. Engelschall in the German computer magazine iX [engelschall.com] both in German and English which is a nice introduction to mod_rewrite.

If you could be more specific about which part, sentence, example etc. you don´t understand in the original mod_rewrite documentation [httpd.apache.org] I´d gladly supply a German translation.

hpche´s rule would only match
http://domain.tld/directory/[searchkeyword]/

RewriteRule ^directory/(.*)$ directory/search.php?search=$1
will match a URL without the trailing slash.

You may also try the German SelfHTML Forum.

Andreas

Robbatse




msg:392027
 3:30 pm on Oct 1, 2002 (gmt 0)

thx alot!

but when I place it in the virtual host field
nothing happens

do I have to add an [R] to it? (I've read in the documentation that an [R] flag redirects you to another url)

andreasfriedrich




msg:392028
 3:32 pm on Oct 1, 2002 (gmt 0)

where do I have to place it?

Whereever you need it. According to the documentation [httpd.apache.org] you may use the RewriteRule directive in the following context (Kontext auf Deutsch, surprise, surprise and easy enough to understand ;)): server config, virtual host, directory, .htaccess

No an R flag is definitely not what you want, since that will cause an external redirect, while all you want is an internal one. Try using the RewriteLog.

Andreas

Robbatse




msg:392029
 3:37 pm on Oct 1, 2002 (gmt 0)

sorry...I am too stupid!

I placed it somewhere in my http.conf and restarted the webserver but I still got an 404 error
the properties of the page shows my written url (http://url/directory/test) but not as it should be: [url...]

very curious :(

Robbatse




msg:392030
 3:46 pm on Oct 1, 2002 (gmt 0)

where can I find the rewrite.log?
there is no such file in my apache directory - do I have to write someting in my http.cfg file?

Slade




msg:392031
 3:48 pm on Oct 1, 2002 (gmt 0)

If you want the user to actually see the redirection from /url/keyword to /url/search.php, then you should add "[L,R=permanent]" to the end of the line.

andreasfriedrich




msg:392032
 6:15 pm on Oct 1, 2002 (gmt 0)

You need to enable logging with the RewriteLog [httpd.apache.org] and RewriteLogLevel [httpd.apache.org] directives.

Andreas

Robbatse




msg:392033
 7:42 pm on Oct 1, 2002 (gmt 0)

hey, I'm one step closer now!

I wrote in my http.cfg:

RewriteEngine on
RewriteRule ^/php/(.*)$ /php/test.php?text=$1 [L,R]

(if I write R=permanent, my browser opens and opens the url again and my log-file gets bigger and bigger - so this won't be a solution ;) )

if I write
[127.0.0.1...]

my browser redirects to

[127.0.0.1...]

why is there "test.php" and not "search" at the end?

what can I do?

another problem appeared:
I have a file called phpinfo.php in the same directory.
it shows me the current php version. but it I enable the rewrite module (and the additional commands) my browser sucks up and can't find it - it tries thousand times to open the file (the same effect if I write [P=permanent])
what could that be?

furthermore, thank you for your help!

jdMorgan




msg:392034
 8:43 pm on Oct 1, 2002 (gmt 0)

Robbatse,

After a rewrite happens, the RewriteRules will be re-evaluated if any external redirects are done - that is, if any [R] flag is present. As long as the pattern in a RewriteRule matches, mod_rewrite will continue to execute and rewrite for each external redirect.

You can add conditions to rewrite rules - see the Apache documentation.

The easiest way to avoid this "looping" is to make sure that the rewritten URL does not match the rewrite pattern:

In other words, rewrite [yourdomain.tld...] to [yourdomain.tld...]

Do not try to rewrite [yourdomain.tld...] to [yourdomain.tld...] - Change it so you won't have another match! The easiest way is to rewrite to a different directory:

RewriteEngine on
RewriteRule ^/php/(.*)$ /newphp/test.php?text=$1 [L,R]

-or-

RewriteEngine on
RewriteRule ^/oldphp/(.*)$ /php/test.php?text=$1 [L,R]

-

As stated above, you can also add conditions to rewrites:

RewriteEngine on
RewriteCond %{REQUEST_URI} !test\.php
RewriteRule ^/php/(.*)$ /php/test.php?text=$1 [L,R]

This will rewrite any URL in path /php/ that does not already contain "test.php"

Sorry if this is not clear - ich nicht sprechen sie deutch! :(

Jim

Robbatse




msg:392035
 6:11 am on Oct 2, 2002 (gmt 0)

great, thx for your help I think I understand it now!

at the moment, I am testing my page on a devel-server (so the directory is /php/... and something else) but later we will run it with a domain without a complex directory-structure in the url. the visitor should just type his searchword after the domain and get to his preferred search result.

I have no problem with your description - be english isn't even better ;)

Robbatse




msg:392036
 6:29 am on Oct 2, 2002 (gmt 0)

ergh...I ment "my english isn't even better" !
this could run me into deeep trouble ;))

hey, now I tried the following:

to avoid, that the rewritten url will not be rewritten again and the text string don't matches the rewrite rule I created a new directory, called "/redir/" and placed a script "redir.php" in it.

but it still goes on with looping because the string "/redir/redir.php?word=text" is rewritten.
maybe I should exclude strings with a ".php?" or something what would you say?

louponne




msg:392037
 7:12 am on Oct 2, 2002 (gmt 0)

This isn't immediately related to the question, but I also have a mod rewrite question.

I have installed a simple rule to serve .php pages as .htm

This works great on my web server, but not on my local testing machine, making it pretty hard to test my site locally! What do I have to do to get this working on my own machine? I have the latest release of apache, win2k. I found a couple of lines in //apache/conf/httpd.conf that mentioned the rewrite module and un-commented them.

But the problem undoubtedly lies with my htaccess file, which I have called htaccess.txt on my local system - of course apache can't recognize that! When I upload it to my web server, I rename it to .htaccess, but I can't name it that in win2k. What to do?

jdMorgan




msg:392038
 3:50 pm on Oct 2, 2002 (gmt 0)

Robbatse,

The key to preventing a loop is to make sure that the rewritten path does not match the requested path, or to not use an external redirect. If [the rewritten path is canonical (includes [...)...] OR there is an [R] flag at the end of the rewrite rule], AND the rewritten path matches the requested path, then you will get a loop unless you add a RewriteCond to stop rewriting (like the one in my post above).

The following should not loop.

Rewrite [domain.tld...] to [domain.tld...] :

RewriteCond %{REQUEST_URI} !phpdirectory/search\.php
RewriteRule requestdirectory/(.*)$ phpdirectory/search.php?=$1 [L]

Jim

58sniper




msg:392039
 2:37 am on Oct 12, 2002 (gmt 0)

Okay, I'm a little lost.

I want to redirect "/article/551" to "/article.php?aid=551"

So I'm trying to use

RewriteEngine on
RewriteRule article/([0-9]+) /get_article.php?aid=$1 [L]

The result is that it DOES redirect to the /get_article.php page, but the variable "aid" is not set (and thus, problems occur). If I manually go to /get_article.php?aid=551, it works fine.

Ideas?

58sniper




msg:392040
 3:05 am on Oct 12, 2002 (gmt 0)

Actually, I got this to work. Here was the issue:

Using

RewriteEngine on
RewriteRule article/([0-9]+) /get_article.php?aid=$1 [L]

I tried to go to /article/551 - but it was actually hitting a file called article.php, and not sending the variable. Once I realized that and deleted the article.php, it worked fine.

jdMorgan




msg:392041
 4:57 am on Oct 12, 2002 (gmt 0)

58sniper,

Sorry, just now saw your earlier post.

It's always the little stuff that gets you (and me)!

Glad to hear you got it working.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / Webmaster General
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved