The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. September 23, 2019, 10:38:45 AM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: htaccess help, directory to querystring  (Read 5410 times)
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« on: June 20, 2008, 06:14:27 PM »

my brain is getting tangled up in regex, and htaccess aint regex...

3 examples of URLs would be                           ->            write to

http://www.site.com/abc                               ->            http://www.site.com/index.php?id=abc
http://www.site.com/abc/                              ->            http://www.site.com/index.php?id=abc
http://www.site.com/abc/somepage.php           ->            http://www.site.com/somepage.php?id=abc

the perm vs temp redirect i don't care. I just cant for the life of me seem to get this work.

this is for client work, and gotta figure this out by monday. I get either 500 errors or 404s no matter how I try it. I am not posting what I have tried, because frankly I dont remember anymore and I am just annoyed as hell right now. Why cant all these damn programs get on the same freaking page... me want real regex....
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
vsloathe
vim ftw!
Global Moderator
Lifer
*****
Offline Offline

Posts: 1669



View Profile
« Reply #1 on: June 20, 2008, 08:51:07 PM »

It's real, just not Perl style Smiley

For this:

http://www.site.com/abc                               ->            http://www.site.com/index.php?id=abc
http://www.site.com/abc/                              ->            http://www.site.com/index.php?id=abc
http://www.site.com/abc/somepage.php           ->            http://www.site.com/somepage.php?id=abc

Give this a go:
Code:
RewriteRule ^(.+)/?$ index.php?id=$1 [L]
RewriteRule ^(.+)/(.+)\.php$ $2.php?id=$1 [L]
Logged

hai
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #2 on: June 21, 2008, 06:15:46 AM »

see thats what I thought as well, but nope, it gives me a 500.

Just to make sure I have it clear. The leading slash not actually matchable right?
http://www.site.com/abc/lkasdkad.php
                           ^not part of the path to be check for matching right?

so i tried a few things.
RewriteRule ^(.*)/$ /index.php?id=$1[L,NC]
works for only the url like: http://www.site.com/abc/

but if I add that question mark after the slash, match 0-1, then i get 500 for any URL.
RewriteRule ^(.*)/?$ /index.php?id=$1[L,NC]

so what I think is happening is a recursive loop. because of the /?
what happens then is the redirect sends the user to the next URL, which is processed again by the same htaccess, which matches either a slash on the end or not. which means, it will always match... lol

but yet, that is what I need it to do, match a slash and do 1 thing, match no slash and do another, or if it ends in a filename or querystring do a third thing.

the querystring step i think I can manage by just matching for a ? in the URL. but its the slash or no slash that is fucking me up.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #3 on: June 21, 2008, 06:40:54 AM »

er ok. i solved it. thanks V, gave clarity.
probably a slightly more elegant way to do this, but I dont give a crap about it. it took way too long already for me. lol

#add these for each directory that actually exists for real, like images, css, downloads, that you dont want rewritten.
RewriteRule ^somedir/(.+)$ somedir/$1 [L,NC]

#matches abc/ and abc but not abc/somepage.php or somepage.php or ?partner=abc
RewriteRule ^([^/\.\?]+)/?$ /index.php?partner=$1 [L,NC]

#matches abc/partnerpage.php
RewriteRule ^([^/\.]+)/(.+)$ /$2?partner=$1 [L,NC]



yeays! Sleep, FTW!
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #4 on: June 27, 2008, 06:08:20 AM »

see thats what I thought as well, but nope, it gives me a 500.

Just to make sure I have it clear. The leading slash not actually matchable right?
http://www.site.com/abc/lkasdkad.php
                           ^not part of the path to be check for matching right?


I just came across this and remembered your post:

"The {REQUEST_URI} string starts with a / character. Apache changed regex engines when it changed versions, so Apache version 1 requires the leading slash while Apache 2 forbids it!"

http://www.sitepoint.com/article/apache-mod_rewrite-examples


 Shocked
Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
nutballs
Administrator
Lifer
*****
Offline Offline

Posts: 5627


Back in my day we had 9 planets


View Profile
« Reply #5 on: June 27, 2008, 06:37:03 AM »

ah.... ok that explains some behaviors I have seen on different servers. thanks bomps. gotta file that one into my little WTF emergency button in my brain.
Logged

I could eat a bowl of Alphabet Soup and shit a better argument than that.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!