Navigate
Home
ArticleWiki
Forum
Journal
Search
Newsletter
Links
Tech News
expertsrt.com
Welcome Guest.
Username:

Password:

Remember me

Stuck with .htaccess
Welcome, Guest. Please login or register.
December 02, 2008, 02:09:18 PM
11304 Posts in 1248 Topics by 498 Members
Latest Member: katCheeme
Experts Round Table Network  |  Webservers  |  Apache  |  Stuck with .htaccess « previous next »
Pages: [1] 2
Author Topic: Stuck with .htaccess  (Read 1421 times)
Esopo
Governing Council Member
*
Offline Offline

Posts: 74


WWW
« on: February 11, 2006, 02:57:33 AM »

After several hours of toying around with my Apache config I finally got it to recognize the .htaccess file of one of the virtualhosts (that I am running tests on). This is a test server on my PC (Apache 2, PHP 4.3, Win2003)

Now, I know the little bugger is working because it yields a 500 error when I write gibberish in the .htaccess file. But then it doesn't do anything with the rules that I am trying to setup. My ultimate goal is to produce a RewriteRule to accept friendly URLs and internally turn them into variables, but right now I'm just trying to "wiggle my big toe" (get the .htaccess to show some signs of life now that I finally got it to show some signs of death).

I'm trying with this code as a test:

Code:
RewriteEngine on
RewriteRule ^/index.html$ /index.php


To the best of my crippled regex knowledge, that rule should turn an "/index.html" request into an "/index.php" request. But when I ask for the file (index.html) I get a 404.

I also tried with other sample rules (similar to that one).

Any advice is highly appreciated. Please! (Is there a button to beg in phpbb?  :wink: )
Logged
Anonymous
Guest
« Reply #1 on: February 11, 2006, 06:24:25 AM »

Do you have an index.php file?
Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #2 on: February 11, 2006, 10:46:05 AM »

I know it's probably just for test purposes, but if your main concern is to get .html requests point to a .php script, you've other solutions in httpd.conf :

1) DirectoryIndex index.php index.html index.htm
2) in-site URI parsing and redirection
3) soft links

regards
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #3 on: February 11, 2006, 10:49:17 AM »

also, have you put [L,NS] at the end of the RewriteRule ? As per the apache documentation ?

you can also look at some samples on google, one nice is http://tomclegg.net/rewriterule
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
GrandSchtroumpf
Mentor

Offline Offline

Posts: 410



« Reply #4 on: February 11, 2006, 11:54:13 AM »

This is what i use:

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php [L]


It makes all ".html" urls that do not exist point to "index.php".  Note the escaped ".", that's important!!!
The I parse the url in "index.php" to know what content i need to return.
Logged
Esopo
Governing Council Member
*
Offline Offline

Posts: 74


WWW
« Reply #5 on: February 11, 2006, 01:21:53 PM »

Quote from: "GrandSchtroumpf"
This is what i use:

Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ index.php [L]



It's alive!!! Thanks for that code, now I know it works.

>>Do you have an index.php file?
Yes  :D, I'm naive with Apache and regex, but it stops there.

Quote
you've other solutions in httpd.conf :
1) DirectoryIndex index.php index.html index.htm
2) in-site URI parsing and redirection
3) soft links


I can play with httpd.conf on my test server, but I won't have that luxury on the final host. I would like to explore any available options though - in the quest for the efficient code! code! code! (superhero voice and echo)

My final goal with this little experiment is to allow visitors to come in through this:
http://www.site.com/first/second/third

and my script receive this:
http://www.site.com/index.php?a=first&b=second&c=third

Or even better, send my script an array with the arguments.

I have a lot of reading to do, thanks for the help and thanks for the links!
 :cheers:
Logged
Esopo
Governing Council Member
*
Offline Offline

Posts: 74


WWW
« Reply #6 on: February 15, 2006, 03:52:06 AM »

After quite a few hours of trying to get the .htaccess to play my song I realized I don't need to do this directly with the server, I can just send the entire received URL to PHP and parse it there.

So this is where I am at. I am trying to get the .htaccess page to send to index.php everything in the link that is not the main domain:

http://www.site.com/first/second/third/
to
http://www.site.com/index.php?received=first/second/third/


So, why won't these work?
Code:
RewriteEngine On
RewriteRule (.*) index.php?received=$1 [L]


Code:
RewriteEngine On
RewriteRule ^(.*)$ index.php?received=$1 [L]


They don't crash the .htaccess, but don't return the link content either. Both return "index.php" and make a mess of my path. I'm guessing I am missing a delimiter of some sort.

This one works:
Code:
RewriteEngine On
RewriteRule (.*)/ index.php?received=$1 [L]

But it chops off the last parameter if the URL doesn't have the trailing /

I just want it to send me everything. Any ideas?
Logged
NeoTeq

Offline Offline

Posts: 21


« Reply #7 on: February 15, 2006, 05:20:35 AM »

What you could also try is split the url in the rewrite rule:

Code:
RewriteEngine On
RewriteRule ^/(.*)/(.*)/(.*)[/]?$ index.php?first=$1&second=$2&third=$3 [L]


That's what I usually do, anyway. If not, you could make the last '/' optional by changing (.*)/ to (.*)[/]?
Logged

Still claiming: There is no peace.
GrandSchtroumpf
Mentor

Offline Offline

Posts: 410



« Reply #8 on: February 15, 2006, 11:50:53 AM »

The original url is available as a php global.  No need to pass  it as a POST parameter.
Use this (as in my previous post):

RewriteRule ^(.*)\.html$ index.php [L]

Then parse this value in php: $_SERVER['REQUEST_URI']
Logged
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #9 on: February 15, 2006, 12:00:38 PM »

Quote from: "GrandSchtroumpf"
The original url is available as a php global.  No need to pass  it as a POST parameter.
Use this (as in my previous post):

RewriteRule ^(.*)\.html$ index.php [L]

Then parse this value in php: $_SERVER['REQUEST_URI']


First : what he wrote would be a GET parameter ;-)

Then : I'm not sure of what you say. Here, we're BEFORE any interaction with PHP (the PHP modeule usually). The mod_rewrite acts BEFORE. I'm not sure PHP will have the ORIGINAL URI in the corresponding $_SERVER variable. I'm pretty sure it will have the modified (rewritten) one...

This said, clearly cutting the URI in pieces $1 ^2 ^3 etc is a no-go. It should work better with something like RewriteRule ^(.*)$ index.php?received=$1

Have you checked that the second parameter of the RewriteRule hasn't to be an URI ? http://$SERVER/newpath/index.php ? I think I remember something about the impossibility of using file paths there. I may be wrong though. Check www.apache.org for the documentation of the server+version you use.
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
VGR
Mentor

Offline Offline

Posts: 682



WWW
« Reply #10 on: February 15, 2006, 12:07:48 PM »

I didn't check myself, but I would use :

RewriteRule ^/(.*)$ /index.php?received=$1 [L]
Logged

techie overlord, answers all kind of questions on http://www.europeanexperts.org
Esopo
Governing Council Member
*
Offline Offline

Posts: 74


WWW
« Reply #11 on: February 15, 2006, 12:13:27 PM »

NT,

Thanks, I already tried (.*)[/]? (along a large number of other combinations using .*+[]\w with ? in the hope of it returning the full string.

And I stopped going for the /(.*)/(.*) etc method because I don’t really want to deal with the what-ifs on Apache. Better it sends everything to my script and I can take it from there.



GrandSchtroumpf,

This: RewriteRule ^(.*)\.html$ index.php [L]

Works great to turn .html requests into .php requests, but that is not what I am trying to achieve (it was at first as a test to make sure my server was working correctly).

I just want to send everything to the script.


VGR,

I did a lot of reading on the Apache site about mod-rewrite and I am basing my code on stuff I grabbed from there. As far as my mod_rewrite knowledge goes, the (.*) should do the trick just fine, except it doesn’t.
 :scratch:
Logged
Esopo
Governing Council Member
*
Offline Offline

Posts: 74


WWW
« Reply #12 on: February 15, 2006, 12:17:04 PM »

Quote from: "VGR"
I didn't check myself, but I would use :

RewriteRule ^/(.*)$ /index.php?received=$1 [L]


Yeah, I tried that one too. For some reason when I add the slash to index.php (as in "/index.php?etc") the .htaccess crashes.

Then when I use the slash at the begining of the regex (as in "^/(.*)$") it doesn't return the first parameter because the string it is using appears to begin without a /, so it returns "second/third/"
Logged
GrandSchtroumpf
Mentor

Offline Offline

Posts: 410



« Reply #13 on: February 15, 2006, 01:14:31 PM »

Quote from: "VGR"
First : what he wrote would be a GET parameter ;-)

Hmmm, HTTP dyslexia I guess.

Quote from: "VGR"
Then : I'm not sure of what you say. Here, we're BEFORE any interaction with PHP (the PHP modeule usually). The mod_rewrite acts BEFORE. I'm not sure PHP will have the ORIGINAL URI in the corresponding $_SERVER variable. I'm pretty sure it will have the modified (rewritten) one...

It works fine, mod_rewrite does not change the headers of the HTTP request.  That's what i use on my own site.

Quote from: "Esopo"
I just want to send everything to the script.

Still no need to use a "GET"... just use this:
Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]

Don't forget the "RewriteCond" statements or ALL your urls will be forwarded to index.php, including urls that point to existing files (like your images).
Logged
Esopo
Governing Council Member
*
Offline Offline

Posts: 74


WWW
« Reply #14 on: February 15, 2006, 02:14:47 PM »

Yes! Brilliant! Brilliant!!

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]


Turn on the rewriter, make sure that before rewriting the requested file doesn't actually exist or is a folder, and then redirect internally to index.php that receives the original URI (and I'm betting it also receives GETs POSTs and COOKIES):

echo($_SERVER['REQUEST_URI']);    // "/first/second/third"

It couldn't have been easier or more perfect. A million points for GrandSchtroumpf and half a mill for everybody else that participated here.

Thank you all!
 :D
Logged
Pages: [1] 2
« previous next »
    Jump to: