PDA

View Full Version : [PHP/HTACCESS] URL Help



Trigs
10-04-2009, 09:21 PM
How do I make URL's like www.mysite.com/user/USERNAME (http://www.mysite.com/user/USERNAME)? I have a usersystem and instead of doing www.mysite.com/users.php?user=USERNAME (http://www.mysite.com/users.php?user=USERNAME), I want the folder type URLs.

Moh
10-04-2009, 09:34 PM
I always get myself muddled up with HTACCESS for some reason :(

But something like this should work, I don't have access to our server atm though, so I cannot test it out.



RewriteEngine On
RewriteRule ^user/([a-zA-Z0-9=_.!-]+)/$ users.php?user=$1 [NC]

Johno
10-04-2009, 09:38 PM
This should help: http://www.habboxforum.com/showthread.php?t=514476

This should work, just modified the code that Source posted:



RewriteEngine on
RewriteRule ^user/([A-Za-z0-9-]+)/?$ users.php?user=$1 [L]


EDIT: Looks like Jack bet me to it!

Trigs
10-04-2009, 09:41 PM
Thanks I'll try it out.

Iszak
10-04-2009, 09:42 PM
I could see why you would add the last rule flag but on Jacks he uses the case insensitive flag when he's defining both lower and upper case letters.. Just doing extra work, mine was.


RewriteRule ^user/([^/]+)/?$ users.php?user=$1

I'm sure you could also do something like.


RewriteRule ^user/(.+?)/?$ users.php?user=$1

Source
10-04-2009, 09:44 PM
The easiest way to do it is with custom routes for each url type (as shown above).

The other option is to redirect everything to index.php (or a php file to handle it) and then use php to handle it; this option, however, is only normally used with frameworks.

Trigs
10-04-2009, 09:46 PM
Okay now there are so many suggestions I don't know which to use.

I want the following:
-Trailing slash is optional
-users.php?user=USERNAME becomes users/USERNAME
-The USERNAME part cannot have any slashes (other than the beginning and trailing slash)

Dentafrice
11-04-2009, 02:45 PM
I could see why you would add the last rule flag but on Jacks he uses the case insensitive flag when he's defining both lower and upper case letters.. Just doing extra work, mine was.


RewriteRule ^user/([^/]+)/?$ users.php?user=$1

I'm sure you could also do something like.


RewriteRule ^user/(.+?)/?$ users.php?user=$1

It wouldn't be a plus though would it? I thought it was (.*?) for non greedy matches?

Iszak
11-04-2009, 03:24 PM
They're both non-greedy, the difference is .* matches zero or more where as .+ matches one or more.

Want to hide these adverts? Register an account for free!