PDA

View Full Version : help mod rewrite /arg to /index.html?arg


uglycoder
06-09-2009, 10:29 PM
guys,

how do you redirect from :

www.mydomain.com/mydir/arg

to :

www.mydomain.com/newdir/index.html?arg

or

www.mydomain.com/cgi-bin/newdir/prog.pl?arg

thanks (this apache stuff's killing me!)

schleppel
06-10-2009, 02:15 AM
Should the URL in the browser address bar change?

Try either
Redirect 301 /mydir/ /newdir/index.html?
Redirect 301 /mydir/ /cgi-bin/newdir/prog.pl?
or
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^mydir/(.+)$ /newdir/index.html?$1 [QSA,L]
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^mydir/(.+)$ /cgi-bin/newdir/prog.pl?$1 [QSA,L]
in a .htaccess file in your document root.

uglycoder
06-10-2009, 04:57 AM
I tried the last option which works great.

However, you mention that I can stop the URL in the browser bar changing on the redirect. How do I do this as i do not want it to change?

schleppel
06-10-2009, 11:33 PM
It shouldn't change if you use either of the mod_rewrite (RewriteRule) options. What is the full code you are using?

uglycoder
06-11-2009, 01:45 AM
Actually i am going to another server which seems to cause the problem as when i go local it is fine. Is there a way to freeze the url even when I go to the new server (code is below)?

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([0-9]+)$ http://www.newdomain.com/cgi-bin/perl_prog.pl?id=$1 [L]

schleppel
06-11-2009, 08:56 PM
You will need mod_proxy. It will use the bandwidth and transfer of both hosts. To use it add the P flag to the end of the RewriteRule ([P,L]).

uglycoder
06-12-2009, 05:41 AM
Sounds good. One more question though. Is there any way to trigger the rewrite from code that is inside an html page that is produced on the server.

i.e. I have a page that contains a search form that when the user clicks the 'Search' button it performs a post to a perl program (/cgi-bin/search.pl) on the same server to perform a search. Instead I want the click of the Search button to perform a post to search/args and have a rewrite rule that intercepts the call to search/args and re-routes it to /cgi-bin/search.pl?args . However when I try this using a standard rewrite rule in .htaccess I get a 404 message that search/ is not found, even when I fully qualify the URL in the action tag of the post. Note that i also tried it without the trailing / and it still failed.

code in html page:
<FORM name='searchform' method="post" action="http://www.mydomain.com/search/">
<input type="text" name="city" value="" />
<input type=submit value=Search>
</FORM>

code in .htaccess on mydomain.com :

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^search/(.+)$ http://www.newdomain.com/cgi-bin/search.pl?$1 [P,L]


Any suggestions?

schleppel
06-17-2009, 12:23 AM
You cannot change the behaviour of a <form> without JavaScript. And you can't rely on JavaScript as some people do not have/use it.

You can redirect requests with mod_rewrite, so you could change it to a method="get" <form> and use
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?city=([^/]+)(&.*)?$ [NC]
RewriteRule ^search/?$ /search/%2? [R=301,L]

RewriteRule ^search/(.+)$ http://www.newdomain.com/cgi-bin/search.pl?$1 [P,L]
I would suggest you use JavaScript as well if you use this method so that most people will not need to be redirected.