PDA

View Full Version : Web Design question - coding



Renmoo
12-06-2011, 08:23 PM
Hey all,

I am trying to create the following, in which when the user types:


http://www.domainname.com/uniqueinformationgoeshere

The "uniqueinformationgoeshere" bit will be copied and pasted into a text field in a index.php document that is stored in the root directory.

How do I do that?

Cheers :)

inphinity
12-06-2011, 09:01 PM
Could do it with URL rewriting, passing the "unique text" to index.php.

Renmoo
12-06-2011, 09:04 PM
Hmmmm, explain a bit more? :)

somebody
12-06-2011, 09:33 PM
Use http://www.iwebtool.com/htaccess_url_rewrite to generate a .htaccess file which can be used to configure URL rewriting.

Renmoo
12-06-2011, 09:42 PM
Right. After generating the .htaccess file, how do I go about retrieving the URL and insert it into the textfield?

somebody
12-06-2011, 10:28 PM
Your index.php file will need to take the argument (for example, blah=myfunnything) and insert it into the input field's value.

Renmoo
13-06-2011, 12:02 AM
I just realised that I omitted a crucial information: The part where it says "uniqueinformationgoeshere" of the link http://www.domainname.com/uniqueinformationgoeshere is generated dynamically (i.e. it is different every time).

In other words, there isn't a set "uniqueinformationgoeshere" that I can enter at the end of the domain name in the .htaccess URL rewrite Enter Dynamic URL textfield.

Chilling_Silence
13-06-2011, 08:43 AM
If it *had* to be /uniqueinformationgoeshere as opposed to /index.php?i=uniqueinformationgoeshere then I'd be outta ideas, but if not, then perhaps this might be of use:
http://www.w3schools.com/php/php_get.asp

inphinity
13-06-2011, 01:37 PM
You can use url rewriting with a regex to pass /anyrandomvalue to index.php.

somebody
13-06-2011, 07:47 PM
I *think* (and haven't test this) that the following .htaccess might do the trick - at least it's a starting point to think about:



Options +FollowSymLinks
RewriteEngine on

RewriteRule /([a-zA-Z]*) index.php?blah=$1


Then you would need some PHP code to retrieve the "blah" variable and stick the value into the text box.

Renmoo
13-06-2011, 07:48 PM
You can use url rewriting with a regex to pass /anyrandomvalue to index.php.
Hmmm... Further explanation? :o

razzarphenix
14-06-2011, 10:27 AM
Hmmm... Further explanation? :o

Somebody just explained it further to you in the last post about how to do it in a .htaccess rule otherwise google mod_rewrite there is plenty of documentation. (http://www.workingwith.me.uk/articles/scripting/mod_rewrite)

In a nutshell:
webserver receives a request to:
http://somedomain.com/someuniqueid
the webserver looks inside your docroot and reads the .htaccess contents finds a matching rule so rewrites the url to be http://somedomain.com/index.php?blah=someuniqueid
which your index.php then processes however you want.

Renmoo
18-06-2011, 04:55 PM
Thank you. After several hours of trial and error, I have accomplished it! :)

somebody
18-06-2011, 04:57 PM
Thank you. After several hours of trial and error, I have accomplished it! :)

Well done!

Renmoo
18-06-2011, 05:50 PM
One more question:



RewriteRule /([a-zA-Z]*) index.php?blah=$1

My understanding is that [a-zA-Z] means that characters in the square brackets will be copied, is that right? What if I want to copy question marks as well?

Cheers :)

inphinity
18-06-2011, 06:55 PM
think you can use ['-?] to match a set of common symbols ('()*+,-./0123456789:;<=>?), so

RewriteRule /([a-zA-Z'-?]*) index.php?blah=$1

should match anything containing alpha, numeric, or the symbols above. However, be careful when passing certain character types to php if you're not handling them correctly.

Renmoo
19-06-2011, 12:57 AM
Hmmmm, that trick didn't seem to work, inphinity.

Just to clarify my query - the unique information that someone types in follows the format below:



view?v=kl34jf


At the moment, index.php can "see" and copy the view bit. I suspect .htaccess can't see the question mark typed and any information contained beyond that.

inphinity
19-06-2011, 01:18 PM
Hmm, maybe Apache doesn't use standard perl regex as I'd expected. Maybe we need
[a-zA-Z0-9?=]*

Erayd
19-06-2011, 06:18 PM
Yes, Apache uses pretty standard regex syntax. The rule you need is this:
RewriteRule ^/(.*)$ /index.php?v=$1 [L,QSA]L means don't apply any more rewrite rules after this one, in case you have something else in your config that's mangling the URL after you've dealt to it.

QSA means append the query string to the rewritten URL, so if you have '/somerandomurl?something=somethingelse', it will be rewritten as '/index.php?v=somerandomurl&something=somethingelse'.

The important thing to note here is the '$' symbol at the end of the regex; that ensures that the captured text contains the whole string, rather than just 0 or more characters of it (because the regex appears to be evaluated as ungreedy, in your case your original regex would have grabbed 0 characters, which is obviously just a tad useless).

Your PHP script should access this via the array variable $_GET['v'].

Alternatively, you can get at it via the $_SERVER['PATH_INFO'] variable by using the following rule:
RewriteRule ^/(.*)$ /index.php/$1 [L,QSA]