Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-21-2011, 04:32 PM   PM User | #1
camaross
Regular Coder

 
Join Date: Jan 2011
Posts: 130
Thanks: 101
Thanked 0 Times in 0 Posts
camaross is an unknown quantity at this point
Friendly url not working

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?

Last edited by camaross; 01-22-2011 at 05:00 AM..
camaross is offline   Reply With Quote
Old 01-21-2011, 05:02 PM   PM User | #2
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
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.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Users who have thanked djm0219 for this post:
toolglutton (01-21-2011)
Old 01-21-2011, 07:55 PM   PM User | #3
120
Regular Coder

 
Join Date: Nov 2009
Location: UK
Posts: 105
Thanks: 6
Thanked 15 Times in 15 Posts
120 has a little shameless behaviour in the past
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 ?
__________________
There is always plenty of idol work for the devils hands to do
Registered Linux User 475075 : Project Honeypot
120 is offline   Reply With Quote
Old 01-21-2011, 08:39 PM   PM User | #4
toolglutton
New Coder

 
Join Date: Dec 2010
Posts: 25
Thanks: 16
Thanked 0 Times in 0 Posts
toolglutton is an unknown quantity at this point
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?




Quote:
Originally Posted by djm0219 View Post
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.

Last edited by toolglutton; 01-22-2011 at 04:57 AM..
toolglutton is offline   Reply With Quote
Old 01-21-2011, 08:46 PM   PM User | #5
djm0219
Senior Coder

 
djm0219's Avatar
 
Join Date: Aug 2003
Location: Wake Forest, North Carolina
Posts: 1,227
Thanks: 2
Thanked 189 Times in 187 Posts
djm0219 is on a distinguished road
What you have should do it though I believe search=$2 needs to be search=$1 instead.
__________________
Dave .... HostMonster for all of your hosting needs
djm0219 is offline   Reply With Quote
Old 01-21-2011, 09:38 PM   PM User | #6
toolglutton
New Coder

 
Join Date: Dec 2010
Posts: 25
Thanks: 16
Thanked 0 Times in 0 Posts
toolglutton is an unknown quantity at this point
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.


Quote:
Originally Posted by djm0219 View Post
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.

Last edited by toolglutton; 01-21-2011 at 10:47 PM..
toolglutton is offline   Reply With Quote
Old 01-22-2011, 04:10 AM   PM User | #7
camaross
Regular Coder

 
Join Date: Jan 2011
Posts: 130
Thanks: 101
Thanked 0 Times in 0 Posts
camaross is an unknown quantity at this point
I also believe it should be able to be achieved.

Basically my question is reduced to how to rewrite php generated urls (http://www.mysite.com/search.php?sea...ubmit=submit)?


Quote:
Originally Posted by toolglutton View Post
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.
camaross is offline   Reply With Quote
Old 01-22-2011, 04:17 AM   PM User | #8
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
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:

Code:
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.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
camaross (01-22-2011)
Old 01-22-2011, 04:46 AM   PM User | #9
camaross
Regular Coder

 
Join Date: Jan 2011
Posts: 130
Thanks: 101
Thanked 0 Times in 0 Posts
camaross is an unknown quantity at this point
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.

Last edited by camaross; 01-22-2011 at 04:56 AM..
camaross is offline   Reply With Quote
Old 01-22-2011, 05:03 AM   PM User | #10
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
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:
Code:
RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
camaross (01-22-2011)
Old 01-22-2011, 05:11 AM   PM User | #11
camaross
Regular Coder

 
Join Date: Jan 2011
Posts: 130
Thanks: 101
Thanked 0 Times in 0 Posts
camaross is an unknown quantity at this point
Thank you for the nice solution. That's refreshing.

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?






Quote:
Originally Posted by ShaneC View Post
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:
Code:
RewriteRule ^([A-Za-z0-9-]+)/submit?$ search.php?search=$1&submit=submit [L]
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L]

Last edited by camaross; 01-22-2011 at 05:13 AM..
camaross is offline   Reply With Quote
Old 01-22-2011, 05:14 AM   PM User | #12
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
Quote:
Originally Posted by camaross View Post
Thank you for the nice solution. That's refreshing.

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 Code:
<?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
}

?>
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
Users who have thanked ShaneC for this post:
camaross (01-22-2011)
Old 01-22-2011, 05:27 AM   PM User | #13
camaross
Regular Coder

 
Join Date: Jan 2011
Posts: 130
Thanks: 101
Thanked 0 Times in 0 Posts
camaross is an unknown quantity at this point
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?


Quote:
Originally Posted by ShaneC View Post
Yup! When it gets to search.php you can just do something like this in your PHP:

PHP Code:
<?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 is offline   Reply With Quote
Old 01-22-2011, 05:34 AM   PM User | #14
ShaneC
Codeasaurus Rex


 
Join Date: Jun 2008
Location: Redmond, WA
Posts: 659
Thanks: 31
Thanked 100 Times in 94 Posts
ShaneC is on a distinguished road
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:

Code:
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L,QSA]
Now, in your search.php, do this for me:

PHP Code:
<?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>.
__________________
Unless otherwise stated, any code posted is most likely untested and may contain syntax errors.
My posts, comments, code, and suggestions reflect only my personal views.
Web Portfolio and Code Snippets: http://shanechism.com
ShaneC is offline   Reply With Quote
The Following 2 Users Say Thank You to ShaneC For This Useful Post:
camaross (01-22-2011), djh101 (01-22-2011)
Old 01-22-2011, 05:44 AM   PM User | #15
camaross
Regular Coder

 
Join Date: Jan 2011
Posts: 130
Thanks: 101
Thanked 0 Times in 0 Posts
camaross is an unknown quantity at this point
Thank you so much. I am working on it. One question, you mentioned to change
Code:
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?


Quote:
Originally Posted by ShaneC View Post
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:

Code:
RewriteRule ^([A-Za-z0-9-]+)/?$ search.php?search=$1 [L,QSA]
Now, in your search.php, do this for me:

PHP Code:
<?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 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:23 PM.


Advertisement
Log in to turn off these ads.