How to create friendly URL in php?

2018-12-31 02:18发布

Normally, the practice or very old way of displaying some profile page is like this:

www.domain.com/profile.php?u=12345

where u=12345 is the user id.

In recent years, I found some website with very nice urls like:

www.domain.com/profile/12345

How do I do this in PHP?

Just as a wild guess, is it something to do with the .htaccess file? Can you give me more tips or some sample code on how to write the .htaccess file?

8条回答
孤独寂梦人
2楼-- · 2018-12-31 02:58

ModRewrite is not the only answer. You could also use Options +MultiViews in .htaccess and then check $_SERVER REQUEST_URI to find everything that is in URL.

查看更多
看风景的人
3楼-- · 2018-12-31 03:04

According to this article, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:

RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1

And this maps requests from

/news.php?news_id=63

to

/news/63.html

Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:

<Files news>
    ForceType application/x-httpd-php
</Files>

And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:

<?php
    echo $_SERVER['PATH_INFO'];
    // outputs '/63.html'
?>
查看更多
谁念西风独自凉
4楼-- · 2018-12-31 03:04

Simple way to do this. Try this code. Put code in your htaccess file:

Options +FollowSymLinks

RewriteEngine on

RewriteRule profile/(.*)/ profile.php?u=$1

RewriteRule profile/(.*) profile.php?u=$1   

It will create this type pretty URL:

http://www.domain.com/profile/12345/

For more htaccess Pretty URL:http://www.webconfs.com/url-rewriting-tool.php

查看更多
无色无味的生活
5楼-- · 2018-12-31 03:11

I recently used the following in an application that is working well for my needs.

.htaccess

<IfModule mod_rewrite.c>
# enable rewrite engine
RewriteEngine On

# if requested url does not exist pass it as path info to index.php
RewriteRule ^$ index.php?/ [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?/$1 [QSA,L]
</IfModule>

index.php

foreach (explode ("/", $_SERVER['REQUEST_URI']) as $part)
{
    // Figure out what you want to do with the URL parts.
}
查看更多
情到深处是孤独
6楼-- · 2018-12-31 03:11

It looks like you are talking about a RESTful webservice.

http://en.wikipedia.org/wiki/Representational_State_Transfer

The .htaccess file does rewrite all URIs to point to one controller, but that is more detailed then you want to get at this point. You may want to look at Recess

It's a RESTful framework all in PHP

查看更多
忆尘夕之涩
7楼-- · 2018-12-31 03:15

It's actually not PHP, it's apache using mod_rewrite. What happens is the person requests the link, www.example.com/profile/12345 and then apache chops it up using a rewrite rule making it look like this, www.example.com/profile.php?u=12345, to the server. You can find more here: Rewrite Guide

查看更多
登录 后发表回答