echo $this->Question("htaccess")

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

echo $this->Question("htaccess")

Post by Gen-ik »

It's probably been asked before ( yes I've searched ) but I'm stumped on something.

If I want to send a variable to the index.php page of my website (from the index.php page) using a method like "Bobs/bits" how can it be done?

In 'real world' terms the actual url would be index.php?Bobs=bits

So, from the site (and when shown in the address bar) a url might look like this.... <a href="Bobs/bits">link</a> but in actual fact it's telling the server to link to index.php?Bobs=bits


Hope that's not too confusing. I know it can be done using htaccess but I'm not sure of the syntax or where to put the .htaccess file.


Any help would be mucho good.
RTT
Forum Commoner
Posts: 38
Joined: Thu Jul 17, 2003 10:22 am
Location: Wolverhampton, UK
Contact:

Post by RTT »

I had to read it twice, but I get what you mean now. I use this method on every website I make, it looks so much better, and is search engine friendly :)

.htaccess:

Code: Select all

<Files bobs>
    Forcetype application/x-httpd-php
</Files>
Then experiment with explode()ing $_SERVER['REQUEST_URI'] :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

also take a look at http://httpd.apache.org/docs-2.0/mod/mo ... ewriterule (... if it is an apache server)
If you set up a rewrite-rule within .htacess (or a <directory> section) remember that the url you have to handle is relative to the position of the rewrite rule, e.g. if your .htaccess is in <documentroot>/myscripts/php and want to catch http://my.serv.er/myscripts/php/bobs/bits you must not install a rule for myscripts/php/(.+)/(.+)$ but (.+)/(.+)$
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Ok cheers guys I'll play around with your ideas :)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

ah, and something that's sometimes forgotten when trying to set up a rewrite rule
Syntax: RewriteRule Pattern Substitution
Context: server config, virtual host, directory, .htaccess
Override: FileInfo
"Override: FileInfo"! You must be allowed to do so ;-)
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

http://www.phpfreaks.com/tutorials/23/0.php - It got me started with what I wanted, hopefully you'll have the same luck as me.
Image Image
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Noooooooo.... so close yet so very far away :cry:

Ok. Things are sort of working now but I keep running into one of two problems depending on what tweaks I do to the .htaccess file. Sometimes I get a a blank page with nothing on it, sometimes I get the correct page showing up but none of the images load up correctly.... I'm guessing this is because the page thinks it's somewhere it's not.


Let's go through it.

The path to the website directory is,
c:/apache/websites/htdocs/urban-republic/

The index page is obviously in the urban-republic folder,
c:/apache/websites/htdocs/urban-republic/index.php

The image folder is also in the urban-republic folder,
c:/apache/websites/htdocs/urban-republic/img/


The mod_rewrite I'm trying to get working is,
Query String URL : some/thing.mod
mod_rewrite URL : index.php?some=thing


The .htaccess file contains,
RewriteEngine On
RewriteRule ^(.*)/(.*).mod /urban-republic/index.php?$1=$2



Help! Please! Once I get to grips with it I should be fine :wink:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably <img src="../img/whatsoever.gif">
The browser (not aware of being served by /urban-republic/index.php) concatenates this to /urban-republic/some/../img/whatsoever.gif => /urban-republic/img/whatsoever.gif
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Thanks volka it's now working fine. All I need to do now is convince my host to allow "Override: FileInfo" which might be a bit more tricky.

At least I know how to do RewriteRule thing now :D
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Erm, sorry... just one more thing.

This is the RewriteRule I'm currently using,

^(.*)/(.*).mod /rewrite/$1.php?run=$2

It keeps saying it can't find the page, but if I replace the $1 with news it works fine... I'm guessing I need to escape something somewhere?

It should work like this,
news/editor.mod -> news.php?run=editor
or
mail/view.mod -> mail.php?run=view


Thanks again, you've all been a massive help.
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

STOP PRESS!

I think I've sorted it out and things are working ok (fingers crossed).
I know it's nothing special but this is what I'm using now...

Code: Select all

RewriteEngine On

# Check for a .mod extension
# --------------------------
  RewriteCond %&#123;REQUEST_URI&#125; \.mod$
  RewriteRule ^(.*)/(.*).mod /rewrite/$1?mod=$2 &#1111;L]

# Normal link execution if URI doesn't contain .exe
# --------------------------------------------------------
  RewriteRule (%&#123;REQUEST_URI&#125;) $1
...... and on another note my Web Host is fantastic, they've done their thing and I can now use Rewrite on-line, and it was done within 20 minutes.


I'm happy :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

;-)
Post Reply