wjohn Posted January 23, 2011 Report Share Posted January 23, 2011 Hello I'm looking to change my urls and generally I just want to have a going on how to do it. First url http://localhost/index.php?action=login I would like that to simply be http://localhost/login But is it then possible to make like http://localhost/admin/adduser And the real link would be http://localhost/admin.php?action=adduser And finally http://localhost/map/x/y would be http://localhost/map.php?x=&y= I'm not asking code for all urls, just a snippet for one of them, and hopefully a link to a good resource. But if someone feel like saving me some time, it's appreciated! (I do not ask for it tho) Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 23, 2011 Report Share Posted January 23, 2011 I'll play around with this a bit - I need to learn this stuff too - but here's a pretty clear tutorial: http://www.easymodrewrite.com/ Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 23, 2011 Report Share Posted January 23, 2011 The first two can be done like this: RewriteEngine On RewriteRule ^login$ index.php?action=login [NC,L] RewriteRule ^admin/adduser$ admin.php?action=adduser [NC,L] The "^" and "$" indicate the beginning/end of the pattern to match. The link above explains this. EDIT: and here is the third rule to add at the end of the .htaccess file: RewriteRule ^map/([0-9]+)/([0-9]+)$ map.php?x=$1&y=$2 [NC,L] this will convert "map/[number]/[number]" to "map.php?x=[number]&y=[number]" Quote Link to comment Share on other sites More sharing options...
wjohn Posted January 24, 2011 Author Report Share Posted January 24, 2011 This seem nice I've tried this, but it doesn't really work RewriteEngine On RewriteRule ^$2$ $1?action=$2 [NC,L] What I',m trying to achive is that i dynamically get it to look like http://localhost/hello http://localhost/wtf is http://localhost/index.php?action=hello http://localhost/index.php?action=wtf The $1 is that I simply want to have more controllers and if it doesnt exists it will just show index.php. But I guess I did something wrong. http://localhost/baby is http://localhost/admin.php?action=baby To avoid problems RewriteEngine On RewriteRule ^$1/$2$ $1?action=$2 [NC,L] Could be solid - but it doesnt work!! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 24, 2011 Report Share Posted January 24, 2011 RewriteEngine On RewriteRule ^$1/$2$ $1?action=$2 [NC,L] I'd need to double check the $1, $2 syntax, but don't you need a .php in there? RewriteEngine On RewriteRule ^$1/$2$ $1.php?action=$2 [NC,L] Quote Link to comment Share on other sites More sharing options...
wjohn Posted January 24, 2011 Author Report Share Posted January 24, 2011 Hmm, I guess it need more edit.. Now: http://localhost/test/index.php/hej What I think is decent would be to get rid of index.php and when it's the default controller let's say index.php i don't think index would be needed, don't you agree? I mean http://localhost/test/hej = http://localhost/test/index/hej Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 24, 2011 Report Share Posted January 24, 2011 RewriteRule ^$1/$2$ $1.php?action=$2 [NC,L] I did a little more research, and you can't use $1/$2 in the first section of the RewriteRule like you have currently. I don't have the time to explain this fully -- I'll post later or you can do some Google research -- but you need to use regular expressions to create those variables, so mod_rewrite knows what to match. Quote Link to comment Share on other sites More sharing options...
wjohn Posted January 25, 2011 Author Report Share Posted January 25, 2011 Hi, I have no problem to wait, I have loads of other stuff to do! Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 25, 2011 Report Share Posted January 25, 2011 I'm actually working on a video tutorial for KillerSites based on this question. I can't say I know everything about this topic yet, but I do think that covering the basics would be useful. This should be what you are looking for: # rewrites "domain.com/test" to "domain.com/index.php?action=test" RewriteRule ^([A-Za-z0-9-_]+)/?$ index.php?action=$1 [NC,L] This section "[A-Za-z0-9-_]" indicates it is looking for upper/lowercase A-Z, numbers and characters "-" and "_". This section "/?" (specifically, the question mark) indicates that the final slash is optional, so "/test/" and "/test" will both work. Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.