开发者

How is made link like this www.facebook.com/Lukavyi?

开发者 https://www.devze.com 2023-04-07 03:05 出处:网络
How to make link like www.facebook.com/Lukavyi ? Must every user have separate php file to have such link? I know, tha开发者_JS百科t you can somehow change url with apache, but is link being changed b

How to make link like www.facebook.com/Lukavyi ? Must every user have separate php file to have such link? I know, tha开发者_JS百科t you can somehow change url with apache, but is link being changed back, when user clicks on it?


This can be done with Apache's mod_rewrite URL rewrite engine. You can specify a URL pattern and direct all requests to a page or PHP script of your liking.

It works by creating a .htaccess file and setting the rules in there. For example:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^users/(.*)$ users.php?username=$1

The first two rows make sure the rewrite engine is enabled, and the third one orders all incoming requests for addresses like /users/MyUserName to be redirected internally to users.php?username=MyUserName. The user will not see the final address, only the "clean" version.

If you don't want the users/ part in the URL and instead want yoursite.com/MyUserName to work instead, you'll have to create a front controller that will handle all incoming requests.


By using .htaccess and mod_rewrite you can handle this.

For example:

You want every user to have it's own URL like www.example.com/user/UserName but want your server to call www.example.com/user.php?name=UserName you create a .htaccess like this:

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

If you get an error or it doesn't work, try adding this code on top of the .htaccess:

Options +FollowSymLinks

Adding the [L] in your .htaccess will prevent your browser from redirecting to user.php?name=UserName and still shows /user/Username but /user.php?name=UserName is used. Using [R] will redirect.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号