...

View Full Version : Friendly url not working



camaross
01-21-2011, 05:32 PM
I created a small search engine (html call for php to do the query) to search a mysql database. For example, if you input 'var1' in the search box of the html, the resulting url shown on my website will be http/www.mysite.com/search.php?search=var1&submit=submit

I am trying to figure out how to change the shown url to www.mysite.com/var1, instead.

Anyone could help?

djm0219
01-21-2011, 06:02 PM
Rewriting is for inbound URLs (those coming from the address bar of the browser) not for outbound URLs (those that result from coming from a form or server side language).

What you have will take the 2nd form you want, the one with DIR/var1 in it, and change it to the original format (search.php?search=var1) not the other way around.

120
01-21-2011, 08:55 PM
Dave is spot on. You are back to front. That said, if you want the URL tidy, why not just use POST data instead of GET ?

toolglutton
01-21-2011, 09:39 PM
I have a similar question. So how do I write the rule so that the incoming /DIR/var1 will be turned into /search.php?search=var1&submit=submit?





Rewriting is for inbound URLs (those coming from the address bar of the browser) not for outbound URLs (those that result from coming from a form or server side language).

What you have will take the 2nd form you want, the one with DIR/var1 in it, and change it to the original format (search.php?search=var1) not the other way around.

djm0219
01-21-2011, 09:46 PM
What you have should do it though I believe search=$2 needs to be search=$1 instead.

toolglutton
01-21-2011, 10:38 PM
I do not agree with you on this.

I do not know how to do that, but I am sure it is possible. For example www.dtcsearch.com.



Rewriting is for inbound URLs (those coming from the address bar of the browser) not for outbound URLs (those that result from coming from a form or server side language).

What you have will take the 2nd form you want, the one with DIR/var1 in it, and change it to the original format (search.php?search=var1) not the other way around.

camaross
01-22-2011, 05:10 AM
I also believe it should be able to be achieved.

Basically my question is reduced to how to rewrite php generated urls (www.mysite.com/search.php?search=var1&submit=submit)?



I do not agree with you on this.

I do not know how to do that, but I am sure it is possible. For example www.dtcsearch.com.

ShaneC
01-22-2011, 05:17 AM
Camaross, is this related to the problem solved in your other thread? Or is it new? If it's related can you update your question based on what we figured out, please. It would help me address your question.

Toolglutton, so are you asking how to take something like http://mydomain.com/dir/var1 and send it through http://mydomain.com/search.php?search=var1 ?

If so, you'll want to utilize something like this:



RewriteRule ^dir/(.*)$ search.php?search=$1 [L]


Both of you may find this newley posted guide helpful: http://www.codingforums.com/showthread.php?t=215977. It's an extensive guide on how to do basic Mod_Rewrite.

camaross
01-22-2011, 05:46 AM
For this thread, I am trying to see if it is possible to rewrite php generated url from 'www.mysite.com/search.php?search=var1&submit=submit' to 'www.mysite.com/var1'?

Thanks.

[QUOTE=ShaneC;1043509]Camaross, is this related to the problem solved in your other thread? Or is it new? If it's related can you update your question based on what we figured out, please. It would help me address your question.

ShaneC
01-22-2011, 06:03 AM
You shouldn't ever need to rewrite from a PHP Query String to the nice version.

When you submit your form submit it to the clean URL. IE: http://mysite.com/var1/submit

And then change your rewrite rules to accommodate the additional entry of /submit.

Change your current section of the Rewrite rules from just this:
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

To this:


RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

camaross
01-22-2011, 06:11 AM
Thank you for the nice solution. That's refreshing. :thumbsup:

I am using a search box (<form action="search.php" method="get" >) inside a html to call for search.php to do the query. How do I modify them so that I can submit the form in a clean url, http://mysite.com/var1/submit?







You shouldn't ever need to rewrite from a PHP Query String to the nice version.

When you submit your form submit it to the clean URL. IE: http://mysite.com/var1/submit

And then change your rewrite rules to accommodate the additional entry of /submit.

Change your current section of the Rewrite rules from just this:
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

To this:


RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

ShaneC
01-22-2011, 06:14 AM
Thank you for the nice solution. That's refreshing. :thumbsup:

I am using a search box (<form action="search.php" method="get" >) to call for search.php to do the query. How do I modify it so that I can submit the form in a clean url, http://mysite.com/var1/submit?

Yup! When it gets to search.php you can just do something like this in your PHP:



<?php

if( isset( $_POST['submit'] ) ){
// The form has been submitted to this address
// Do all the neccessary POST stuff here.
}else{
// Do all the non-POST stuff here
}

?>

camaross
01-22-2011, 06:27 AM
The url does not change. Actually, the submit input is not that important. Inside php, I can remove everything related to $_GET['submit'], and they still work fine.

Don't you think the search.php?search=var1&submit=submit is generated by the html?



Yup! When it gets to search.php you can just do something like this in your PHP:



<?php

if( isset( $_POST['submit'] ) ){
// The form has been submitted to this address
// Do all the neccessary POST stuff here.
}else{
// Do all the non-POST stuff here
}

?>

ShaneC
01-22-2011, 06:34 AM
Whoops! Didn't notice your method there, I thought it was POST. My apologies, I misunderstood.

Yes, that's generated by the page and I don't believe there's a way to change that in the HTML. You can, however, cheat.

Do like we did before, set the action to action="/var1/". You'll also need to change your Rewrite Statements to include the QSA flag.

Take out what I just told you to put in (RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]). Replace that block with this:



RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L,QSA]


Now, in your search.php, do this for me:



<?php

if( isset( $_GET['submit'] ) ){
header( 'location: /' . $_GET['search'] );
exit();
}

// Rest of your code...


This will then take whatever has been submitted through the form and then redirect your browser to http://mysite.com/<Search Value>.

camaross
01-22-2011, 06:44 AM
Thank you so much. I am working on it. One question, you mentioned to change
action="/var1/". But there is no such thing as var1 inside the html. It is basically the <input type="text" name="search"> value.

Am I correct?



Whoops! Didn't notice your method there, I thought it was POST. My apologies, I misunderstood.

Yes, that's generated by the page and I don't believe there's a way to change that in the HTML. You can, however, cheat.

Do like we did before, set the action to action="/var1/". You'll also need to change your Rewrite Statements to include the QSA flag.

Take out what I just told you to put in (RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]). Replace that block with this:



RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L,QSA]


Now, in your search.php, do this for me:



<?php

if( isset( $_GET['submit'] ) ){
header( 'location: /' . $_GET['search'] );
exit();
}

// Rest of your code...


This will then take whatever has been submitted through the form and then redirect your browser to http://mysite.com/<Search Value>.

ShaneC
01-22-2011, 06:47 AM
Well yes, var1 can really be anything. Frankly at this point it's just a dummy string which the user won't see after the redirect.

You can just as easily change var1 to "searching".

camaross
01-22-2011, 06:53 AM
Hi ShaneC,

It worked!!! :D

I am very grateful for your big help. Thank you so much for staying up late in helping me out. You did a huge favor for me. :thumbsup:

All the best.

ShaneC
01-22-2011, 06:55 AM
Glad I could help! Best of luck implementing it and please come back if you need assistance in the future!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum