Jump to content

.htaccess mod rewrite help pls!


thewooleymammoth

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

  • 2 weeks later...

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 :*(

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.