?
Solved

HTML forms and htaccess altering visible URL

Posted on 2010-08-17
10
Medium Priority
?
352 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 1332 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 668 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 1332 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

770 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