Solved

HTML forms and htaccess altering visible URL

Posted on 2010-08-17
10
342 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

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…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
In this tutorial viewers will learn how to embed videos in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <video> tag to insert a video. Define the src as the URL of your video; this is similar to …
The viewer will learn how to count occurrences of each item in an array.

929 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

11 Experts available now in Live!

Get 1:1 Help Now