Solved

HTML forms and htaccess altering visible URL

Posted on 2010-08-17
10
346 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What does GoogleTagMgr javascripts below do 5 30
innerHTML 7 34
Hide un-named HTML Label with CSS 14 36
Load data upon clicking a button 8 34
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
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 dynamically set the form action using jQuery.

840 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