Forum Home
Press F1
 
Thread ID: 118581 2011-06-12 08:23:00 Web Design question - coding Renmoo (66) Press F1
Post ID Timestamp Content User
1208544 2011-06-13 07:48:00 You can use url rewriting with a regex to pass /anyrandomvalue to index.php.
Hmmm... Further explanation? :o
Renmoo (66)
1208545 2011-06-13 22:27:00 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. (www.workingwith.me.uk)

In a nutshell:
webserver receives a request to:
somedomain.com
the webserver looks inside your docroot and reads the .htaccess contents finds a matching rule so rewrites the url to be somedomain.com
which your index.php then processes however you want.
razzarphenix (2626)
1208546 2011-06-18 04:55:00 Thank you. After several hours of trial and error, I have accomplished it! :) Renmoo (66)
1208547 2011-06-18 04:57:00 Thank you. After several hours of trial and error, I have accomplished it! :)

Well done!
somebody (208)
1208548 2011-06-18 05:50:00 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 :)
Renmoo (66)
1208549 2011-06-18 06:55:00 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.
inphinity (7274)
1208550 2011-06-18 12:57:00 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.
Renmoo (66)
1208551 2011-06-19 01:18:00 Hmm, maybe Apache doesn't use standard perl regex as I'd expected. Maybe we need
[a-zA-Z0-9?=]*
inphinity (7274)
1208552 2011-06-19 06:18:00 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]
Erayd (23)
1 2