Solved

HTML forms and htaccess altering visible URL

Posted on 2010-08-17
10
341 Views
Last Modified: 2013-11-19
hopefully this will make sense, here is what I am trying to achieve.

I have an input box (search box) on a form on a dynamic html page (index.php). it writes to variable 'param' on submit.

on submit I need to dynamically create a web page, (easy) I guest use $_REQUEST['param'] in my php.

but what I  need to do is redirect the page to
mydomain.com/value/
where value is the user entered text in the input box

this is where I am struggling, what is the best way to achieve this.

note, I currently have entry in htaccess that does the other way around, ie
mydomain.com/value/ is changed to mydomain.com/index.php?param=value
0
Comment
Question by:thydzik
  • 3
  • 2
  • 2
10 Comments
 
LVL 8

Assisted Solution

by:McNetic
McNetic earned 333 total points
ID: 33461871
If you can rely on javascript being enabled, the simplest way would be to change the action property of the form in an onsubmit handler prior to sending. This will result in the browser directly navigating to the correct url. If you don't want to rely on js, you can send a single Location header in php (look below). This has the drawback that the browser will first receive the page with the location header, then the 'real' page, but usually, this is fast and the user doesn't notice.
header("Location: mydomain.com/value/");

Open in new window

0
 
LVL 11

Accepted Solution

by:
thydzik earned 0 total points
ID: 33461963
thank you for this, but I would prefer not to rely on javascript.
I have almost achieved this by using a second htaccess rewrite rule.
however, because I have the 2 doing exactly the same, it seems to be in an infinite loop.

could you please suggest how to prevent this.
code attached.
RewriteCond %{QUERY_STRING} ^param=([a-zA-Z0-9_]+)$

RewriteRule ^index\.php$ /%1? [R,L]



RewriteRule ^([a-zA-Z0-9_]+)$ index.php?param=$1 [L]

Open in new window

0
 
LVL 9

Assisted Solution

by:Snarfles
Snarfles earned 167 total points
ID: 33462152
What McNetic said I think is what you need. I don't know why you need to do it in the htaccess.
header("Location: mydomain.com/".$_REQUEST['param']."/"); 

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 11

Author Comment

by:thydzik
ID: 33462207
okay, but how do I stop it looping,
the idea is that each value search will have its own url, mydomain.com/value/

so mydomain.com/value/ will really be mydomain.com/index.php?param=value

if I implement
header("Location: mydomain.com/".$_REQUEST['param']);
it will continue to loop
0
 
LVL 9

Expert Comment

by:Snarfles
ID: 33462223
Aaaah. Ok Sorry I missunderstood your question :)

I'll have a think but I'm not expert on the htaccess.
0
 
LVL 11

Author Comment

by:thydzik
ID: 33462330
okay, I have solved this in the htaccess by placing a dummy variable in one of the cases.

but could I please get a 3rd experts opinion as to if it is better to implement this in htaccess or via the location header.
RewriteCond %{QUERY_STRING} ^param=([a-zA-Z0-9_]+)$
RewriteRule ^index\.php$ /%1? [R,L]

RewriteRule ^([a-zA-Z0-9_]+)$ index.php?param=$1&a [L]

Open in new window

0
 
LVL 8

Assisted Solution

by:McNetic
McNetic earned 333 total points
ID: 33462571
If it works as intended with the RewriteRules, this solution is technically equivalent to the Location: header. mod_rewrite then does redirects in the same way, by sending this header. Probably, your solution is faster, as php processing is left out, and the rewrite engine is processed anyways.
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
This article describes how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
In this tutorial viewers will learn how to code links for mobile sites that, once clicked, send a call or text to a specified number. For a telephone link (once clicked, calls a number), begin with a normal "<a href=" link tag. For the href, specify…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now