Jump to content

htaccess, mod_rewrite


wjohn

Recommended Posts

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)

Link to comment
Share on other sites

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]"

Link to comment
Share on other sites

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!!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

Loading...
×
×
  • Create New...