Solved

HTML forms and htaccess altering visible URL

Posted on 2010-08-17
10
351 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
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 style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
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…

717 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