Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

apache - How to change appearance of URL from within a PHP script

I have some PHP and HTML in the same file, and I am not exactly sure how to make the URL appear as the name of that page.

Here is an example of what I would like to do: lets say some page id = 1 and the title of that page is HelloWorld.

Instead of site.com/page.php?id=1 I would like the url to appear as site.com/HelloWorld

But the problem I am having is that I only get to know the title of the page inside that page after I query for the title by id.

Considering the setup I described, is there a way to make the urls appear as the names of the pages? And also, if someone links to that page by using the better looking url with the name of the page instead of the id, is there still a way to get the id and by that, the rest of the page contents?

Thanks!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What you need is to learn more on how .htaccess work.

Here is a good link that got me started:

.htaccess tricks and tips

Update:

Here is a very common practice in many framework where all requests are sent to index.php, and from there you use php to serve the correct page:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.site.com/index.php [L,R=301]
</IfModule>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...