-1

I am developing my login system a bit further to look a lot more professional, and I wanted to know how I could turn get requests into simple links so they look a lot more sleeker?

For example for one of my systems a user can search someones elses profile by going to http://www.example.com/user?user=JimmyJones

Thats all fine and dandy but I don't think it looks very good and many other websites don't have this in their links due to some kind of trick I don't know about, as you can see I have gotten rid of the .php at the end which is done using some very simple htaccess.

But how can I change that link above to:

http://www.example.com/user/JimmyJones

Thank you very much for taking your time to read this and I really hope someone can help me out with my little problem, I assume there is some way to do this in .htaccess?

EDIT:

Here are some websites that do it just about how I would like to do it:

imgur.com/user/example

facebook.com/exampleuser

BigHoss
  • 1
  • 3
  • 2
    you're looking for **url rewriting**. It is usually performed through an apache module, `mod_rewrite`. https://httpd.apache.org/docs/current/en/mod/mod_rewrite.html – Calimero Sep 14 '17 at 20:03
  • @Calimero How would I do this sir, I would be very grateful if you could give me some guidance of what I would write to do this? – BigHoss Sep 14 '17 at 20:05
  • 1
    Or you could use a router and the front controller pattern. – Progrock Sep 14 '17 at 20:05
  • @BigHoss you'll probably find more than you need right here on other questions with the associated tag : https://stackoverflow.com/questions/tagged/mod-rewrite :-) – Calimero Sep 14 '17 at 20:06
  • @Progrock I would prefer to use htaccess to do this, but I don't know where I would start or what to write? – BigHoss Sep 14 '17 at 20:07
  • @Calimero I added that tag in, thanks for the feedback there! (I don't know where I would look to find the specific answer I need thought) – BigHoss Sep 14 '17 at 20:09
  • Possible duplicate of [How to make Clean URLs](https://stackoverflow.com/questions/34048235/how-to-make-clean-urls) – Matt S Sep 14 '17 at 20:21
  • 2
    Possible duplicate of [Reference: mod\_rewrite, URL rewriting and "pretty links" explained](https://stackoverflow.com/questions/20563772/reference-mod-rewrite-url-rewriting-and-pretty-links-explained) – Don't Panic Sep 14 '17 at 20:24

2 Answers2

0

In .htaccess assuming the Apache web server with the rewrite module enabled, something like this:

RewriteEngine On
RewriteRule ^user/([a-zA-Z]+)$ user.php?user=$1 [L]

The first line says use the rewrite engine.

The second, says match a url that begins (or rather is relative to the .htaccess containing folder) with the pattern 'user' followed by a slash and matching a pattern of any alphabetic characters until the end of the string (not including additional query parameters).

The L flag basically says job done.

If the .htaccess were in the public root:

//example.com/user/JimmieJones

would map to:

//example.com/user.php?user=JimmieJones

However it will not match:

//example.com/user
//example.com/user/
//example.com/user/JimmieJones/
//example.com/user/Freddy_K9

Note that any existing links in your application:

<a href="/user?user=JimmieJones">Visit Jimmie's Profile</a>

Would likely need to be updated. And with the example pattern above, the old style urls (previously indexed/bookmarked) could fail without your existing rule. You may need to adapt the pattern or set up redirects for the old style.

Managing lots of redirects and rewrites can become a headache. I'd advice some attention to your url name spacing. And documenting them.

I reserve first level patterns for aliases/shortcuts/campaigns.

//example.com/slug

I'd avoid that for your user profile urls if possible.

You'll ideally want to aim for consistency and have one-one correspondence for URLs(with associated http method) and resources (canonical urls).

Progrock
  • 7,373
  • 1
  • 19
  • 25
0

you make .htaccess file in the root dictionary then start it with

RewriteEngine on

then write your rules, For your example it would be like this

RewriteRule ^/?user/([^/]+)/?$ user?user=$1 [L,QSA]

so a full page would be like this

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

just for your example.

AXAI
  • 706
  • 6
  • 17