Help with mod_rewrite

VJP319
VJP319 used Ask the Experts™
on
I have what would seem to be a simple problem with mod_rewrite, but I cannot figure it out:

I want to take URLs that look like this:

http://www.mysite.com/cgi-bin/script.cgi?123&abc

and make them look like this:

http://www.mysite.com/terms/abc

I made sure mod_rewrite was compiled into Apache.  Then I added the following code to httpd.conf and restarted Apache:

-------------------------------------------------------------
LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c

RewriteEngine on

RewriteRule /cgi-bin/mod_rewrite_test.cgi?([0-9]*)&([0-9a-zA-Z]*) /terms/$2
---------------------------------------------------------------

but then I go to http://www.mysite.com/terms/abc and I get a 404.

Please help!


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Is there a file called /terms/abc from the Document Root?

Author

Commented:
no

Commented:
I'm sorry but now I'm completely confused. You're rewriting a URL into another which does not exist?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
Maybe I am the one who is completely confused - I am a little new to this.  Let me try to re-explain what I want.

http://www.mysite.com/terms/abc does NOT exist.  I do not want to rearrange my physical directory structure to make this exist.  I just want my site to be more friendly for search engines, so I am trying to get 'abc' into the URL.

I have a lovely cgi-script which takes 'abc' as a parameter.  This is the page that I want to appear at http://www.mysite.com/terms/abc.  I was under the impression that I could use mod_rewrite to trick the browser into fetching http://www.mysite.com/cgi-bin/mod_rewrite_test.cgI?123&abc when a request is made for http://www.mysite.com/terms/abc.

Can this be done?

Thanks.
Commented:
Yes :-

LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c

RewriteEngine on
RewriteRule  ^/terms/([a-zA-Z]*)$   /cgi-bin/mod_rewrite_test.cgi?123&$1  [L]

maps EXACTLY /terms/abc into /cgi-bin/mod_rewrite_test.cgi?123&abc and no more. I suspect that in real life your "abc" part will be a bit more than just letters, but there's a start.

Author

Commented:
Does it matter how many spaces I have between "RewriteRule  ^/terms/([a-zA-Z]*)$" and   "/cgi-bin/mod_rewrite_test.cgi?123&$1  [L]"?

Commented:
No, not exactly.

In the "pattern-to-be-matched" the ^ sign denotes the start of the string and the $ sign the end. If a space MUST come in an URL then it should be represented as a plus (+) and escaped since plus is a special character (\+).

There must be whitespace between the new pattern and the [] sequence. The latter is optional of course. The L here means "last", the URL is complete and should be used.

I only use space characters for "whitespace". You never know if people have implemented the tab case property :)

Author

Commented:
Thanks!  It is working.  I don't suppose there is any way to pass the 123 to my cgi script without including in the first url, as per my original question?

Commented:
The input URL does not contain 123. The new URL does. Where do you want this 123 and what's it for?

Author

Commented:
The 123 represents a numberical ID parameter that I need for my cgi-script, but I don't want to show it to the user if I dont have to.  It can potentially be any number.

Commented:
OK. But I don't see how the user sees it? My rewrite rule hides it. What EXACTLY have you in the rewrite rule?

Author

Commented:
Nevermind, I  think what I was describing is impossible.  I wanted to somehow pass a dynamic numeric parameter to the cgi script without it appearing in the re-written URL.  If you have any thoughts, please let me know, but for now I will consider the question answered.  Thanks a lot, I really appreciate your assistance BigRat.

Commented:
"...without it appearing in the re-written URL."

Do your rewritten URLs appear in the browser's address bar??? If so you have not used EXACTLY my rewrite rule. Anything starting with http: or an [R] will cause a redirection to occur at the browser.

Author

Commented:
When I go to http://www.mysite.com/terms/BlahBlahBlah/1853 it displays a page as if I had gone to http://www.mysite.com/cgi-bin/mod_rewrite_test.cgi?1853&BlahBlahBlah which is what I wanted.  The URL remains at http://www.mysite.com/terms/BlahBlahBlah/1853.  I am happy with this.  

So yes, my rewritten URL appears and remains in the browser's address bar when I go there.  Isn't this the purpose of mod_rewrite?

Thanks again for all your help.

Commented:
OK, just checking that things were exactly as I thought they should be.

Glad to have been of assistance.

Commented:
"Anything starting with http: or an [R] will cause a redirection to occur at the browser. "


Is there any way to make Apache still show the "clean" URL's even if the RewriteRule has a http://
starting with it... the reason why this is an issue is I wish to run apache webserver on a machine and have a php script on another machine, such that the cleaned up URL's would point to the machine with the php script while at the same time masking the URL's so the parameters cannot be seen by the average user.

example

machine1.myhost.com/cars/tires

points to

machine2.myhost.com/getInfo?C=Cars&P=Tires


is this possible?

Commented:
Use a rewrite rule to forward proxy the request. You should post a question to get the required rule. Asking questions on PAQed questions is cheating.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial