View Full Version : mod rewrite - clean urls
mmoser
04-04-2009, 04:14 PM
I'm trying to get:
http://blarg.com/parm1/parm2
to load
http://blarg.com/sec.php?p=parm1&plgn=parm2
I can get it to load the first parm, but the second parm always tries to put my in the parm1 directory.
this is what I've worked up for a single parm:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ http://%{HTTP_HOST}/$1
RewriteRule ^([A-Za-z0-9-]+)$ /sec.php?p=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) index.php
help please, thanks
wysiwyg
04-04-2009, 09:45 PM
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /sec.php?q=$1&plgn=$2 [L]
cubbieco
04-04-2009, 09:49 PM
It may not be as elegent but I've always done this kind of thing inside of my php script. Something like $http_server_vars["request_uri"] and parse that. Seems easier and you have to parse it at some point anyway.
mmoser
04-05-2009, 10:54 AM
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /sec.php?q=$1&plgn=$2 [L]
Thanks again for coming to my rescue, however this is just loading index.php.
does [^/] grab everything but a slash?
is there a way to "debug" these rules? something I could pass the "url" to, and see what its really doing? I'm having a hard time grasping this I think because I am not getting expected results. I tried looking at the apache access logs, but it just displays the url I'm sending, I don't know how to see what the rule is outputting.
It may not be as elegent but I've always done this kind of thing inside of my php script. Something like $http_server_vars["request_uri"] and parse that. Seems easier and you have to parse it at some point anyway.
first, $http_server_vars is depreciated, you should start using $_SERVER (http://us2.php.net/manual/en/reserved.variables.server.php)
second, I think that is a good idea. Anyone know which is better performing, htaccess or in php?
thanks both of you!
wysiwyg
04-05-2009, 01:23 PM
I use the redirect flag [r=301] to check what internal redirects are doing.
[^/] is a negative character set that matches anything that isn't a forward slash.
^ - matches start of string
([^/]+) - matches one or more not-forward slashes, captures group into backreference 1
/ - matches a forward slash literally
([^/]+) - matches one or more not-forward slashes, captures group into backreference 2
/? - matches zero or one forward slashes
$ - matches end of string
It only matches fu/bar optionally followed by a slash. If you need more specific rules, you need to spell out exactly what it should match.
The second section in your htaccess file says if the requested url is an existing file or directory, pass it through, otherwise redirect everything to index.php.
It's hard to say whether parsing the directories out of the url would be faster than using htaccess rules, but it would be less flexible.
mmoser
04-11-2009, 10:25 AM
ok, I'm sorry if I've misinformed you. I thought I was giving all the relevant information, so now I'm going to fully disclose ;)
This site is currently in a sub directory (test site, prod is in root)
Full .htaccess is here:
AddType nothing/nadda .mp3 .MP3
RewriteEngine on
RewriteBase /vineyard
#loop stopping code.
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
#go to admin cleanly
RewriteRule ^admin$ /admin/ [L]
#microsite
RewriteRule ^pastorsretreat/?$ /micro/pastors08/ [R=301,NC,L]
RewriteRule ^mexico/?$ /micro/mexico/ [R=301,NC,L]
RewriteRule ^5Krun/?$ /micro/5krun/ [R=301,NC,L]
#clean URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?([^/]*)/?$ sec.php?p=$1&plgn=$2 [R=301,NC,L]
#RewriteRule ^([^/]+)/?([^/]*)/?$ sec.php?p=$1&plgn=$2 [NC,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) index.php
currently it works, but only with the R flag. I really want it to just load the correct page and not redirect. Is that possible?
one other thing I changed from your original was RewriteCond %{REQUEST_FILENAME} !-d to RewriteCond %{REQUEST_FILENAME} !-f
it was really blind trial and error, but with the condition !-d I got an error about to many redirects.
I really appreciate the help. Thanks!
mmoser
04-11-2009, 11:10 AM
ok, I'm all kinds of wrong again.
something in the rules I've made has broken all my other redirects or folders..
..just when you think your starting to understand something....
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.