thewooleymammoth Posted February 9, 2010 Share Posted February 9, 2010 I am fairly new to mod rewrite & regex so im sorry if this is dumb. I am trying to change my urls to be cleaner. I need to use get variables but i dont want the /*/index.php?this=that&what=where. I was wondering if its possible to use mod rewrite to change the index of the get variables to say 1 2 3 4 etc... so it would look like this /*/var1/var2/var3/ and my php could be <?php $var1=$_GET['1']; $var2=$_GET['2']; $var3=$_GET['3']; ?> so far in my .htaccess i have <IfModule mod_php5.c> #append and prepend files php_value auto_prepend_file /home/ericwo5/public_html/miners/config.php php_value auto_append_file /home/ericwo5/public_html/miners/site.php RewriteEngine on RewriteRule ^*/([0-9][0-9])$ /*/$1/ [R] RewriteRule ^*/([0-9][0-9])/$ /*?*=$1 </IfModule> Beyond this im a little stumped. Cant find any good tutorials on how to achieve this either. Let me know if you know of one please! What i have said makes sense to me, but i have a biast opinion. Let me know if its not clear enough. thanks Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/ Share on other sites More sharing options...
cags Posted February 10, 2010 Share Posted February 10, 2010 Given a 'Pretty URL' of http://yoursite.com/tom/dick/harry/, that you wish to actually show the page http://yoursite.com/index.php?var1=tom&var2=dick&var3=harry. RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)/$ /index.php?var1=$1&var2=$2&var3=$3 Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1009980 Share on other sites More sharing options...
thewooleymammoth Posted February 10, 2010 Author Share Posted February 10, 2010 thats cool, thanks, but what about if i want to apply it to every page? would ^([a-z]+)/([a-z]+)/([a-z]+)/$ /*.php?var1=$1&var2=$2&var3=$3 work? and just to clarify, those ranges inside the parantheses use anything in the old url that start with a $ and end with a &? or am i missing something? Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1010136 Share on other sites More sharing options...
cags Posted February 10, 2010 Share Posted February 10, 2010 It's basic regular expressions, if your not familiar with them I suggest you do a bit of reading on them. Working with mod_rewrite without knowing Regular Expressions would be like trying to draw without eyes. You might eventually stumble across the desired outcome, but you won't know exactly how you did it or be able to repeat the process. You start with the RewriteRule directive. The second section is a Regular expression that matches the URLs you wish to forward/rewrite. The 3rd section is the page you wish the request to be forwarded to using back references from the regular expression. The rule I provided will forward any URL that consists of 1 or more letters followed by a forward slash followed by 1 or more letters followed by a forward slash followed by 1 or more letters followed by a forward slash followed by nothing. It will be forwarded to an address using the 3 sections of letters between the forward slashes as query_string GET variables var1, var2 and var3. Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1010145 Share on other sites More sharing options...
thewooleymammoth Posted February 11, 2010 Author Share Posted February 11, 2010 Alright i think i get it. I know im pretty bad with regular expressions. I do need to read up on it a little better. Where i got most confused though is the order of the urls. thanks for your help. Ill look into regex Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1010464 Share on other sites More sharing options...
thewooleymammoth Posted February 24, 2010 Author Share Posted February 24, 2010 So This works, RewriteEngine on RewriteBase / RewriteRule ^([a-z]+)/$ index.php?page=$1 [L] but this does not RewriteEngine on RewriteBase / RewriteRule ^([a-z]+)/([a-z]+)/$ index.php?page=$1&v2=$2 [L] can anyone help me figure out why? Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1017259 Share on other sites More sharing options...
thewooleymammoth Posted February 24, 2010 Author Share Posted February 24, 2010 So This works, RewriteEngine on RewriteBase / RewriteRule ^([a-z]+)/$ index.php?page=$1 [L] but this does not RewriteEngine on RewriteBase / RewriteRule ^([a-z]+)/([a-z]+)/$ index.php?page=$1&v3=$2 [L] can anyone help me figure out why? k i figured out why, cause i was testing with the url /tom/2/ and since 2 isnt a a-z it wasnt working. My question now is, how do i get it to redirect to just /tom/ if there is no 2 that trails, do i just add the line over and over and over again? also im getting a strange occurance <IfModule mod_rewrite.c> Options +FollowSymLinks RewriteEngine on #RewriteBase / RewriteRule ^(.*.)/$ index.php?page=$1 RewriteRule ^(.*.)/(.*.)/$ index.php?page=$1&v2=$2 RewriteRule ^(.*.)/(.*.)/(.*.)/$ index.php?page=$1&v2=$2&v2=$3 </IfModule> is my .htaccess foreach($_GET as $k=>$v) echo "$k - $v <br />"; is my relavent page code. When i just do the first url EX: /tom/ I get page - tom When i go to /tom/2/ i get page - index.php v2 - 2 which is really weird to me. Why is it switching from "tom" to "index.php"? Im still pretty new to this so this is very frustrating :*( Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1017265 Share on other sites More sharing options...
wildteen88 Posted February 24, 2010 Share Posted February 24, 2010 These rules need to be in reverse order RewriteRule ^(.*.)/$ index.php?page=$1 RewriteRule ^(.*.)/(.*.)/$ index.php?page=$1&v2=$2 RewriteRule ^(.*.)/(.*.)/(.*.)/$ index.php?page=$1&v2=$2&v2=$3 Also you should set the Last flag too ([L]) after your rules. I'd set your rules as the following # Matches /tom/2/abc123/ RewriteRule ^([a-z\-_]+)/([0-9]+)/([a-z0-9\-_]+)/$ index.php?page=$1&v2=$2&v2=$3 [NC,L] # Matches /tom/2/ RewriteRule ^([a-z\-_]+)/([0-9]+)/$ index.php?page=$1&v2=$2 [NC,L] # Matches /tom/ RewriteRule ^([a-z\-_]+)/$ index.php?page=$1 [NC,L] ([a-z0-9\-_]+) - Matches any character that is a letter (a to z), or a number (0 to 9), a hyphen or an underscore ([a-z\-_]+) - Matches any character that is a letter (a to z), a hyphen or an underscore ([0-9]+) - Matches any character that is a number (0 to 9) Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1017591 Share on other sites More sharing options...
thewooleymammoth Posted February 26, 2010 Author Share Posted February 26, 2010 awesome thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/191542-htaccess-mod-rewrite-help-pls/#findComment-1018314 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.