htaccess以及PHP - 重定向和pretty的网址 [英] htaccess and php - redirects and pretty urls
问题描述
我有一个webcommunity,而现在不断增长的。我喜欢做一个链接改造为我的网站,然后我需要知道我的情况下,最好的解决方案。
I have a webcommunity, and it's growing now. I like to do a link makeover for my web, and then I need to know the best solution for my case.
现在我的htaccess看起来有点像这样的:
Right now my htaccess looks kind of like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?page=user&username=$1 [L]
您可以链接到用户喜欢这样的 domain.com/username ,这就是好的。
You are able to link to users like this domain.com/username and that's nice.
然后,我有不同的页面像
Then I have different pages like
-
?
- 在index.php页面=论坛和ID = 1 ?
- 在index.php页面= someotherpage和ID = 1&安培; anotherid = 5
- 的index.php?网页= 3
...等等。我想他们是这个样子:
... and so on. I want them to look something like this:
- domain.com/forum/23/title-of-the-thread
- domain.com/page2/id1/id2
...等等。
我如何让这些pretty的网址不删除我的domain.com/username功能?你会建议什么解决办法吗?
How do I make these pretty urls without removing my domain.com/username functionality? What solution would you suggest?
我在想创建检查URL,如果它匹配任何页面,用户等方面的文件。然后,它会重定向一个头的位置。
I was thinking about creating a file that checks the URL, if it matches any pages, and users and so on. Then it will redirect with a header location.
推荐答案
如果你打算重写要在同一终点的网址,你可以简单地使用:
If all of the urls you are going to rewrite are going to the same end point, you could simply use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
在index.php文件:
in index.php:
<?php
$url = $_SERVER['REQUEST_URI'];
您如何使用请求URI是你的,例如,你可以使用一个简单的strpos检查:
How you use the request uri is up to you, you could for example use a simple strpos check:
<?php
$url = $_SERVER['REQUEST_URI'];
$rules = array(
'/forum/' => 'forum',
'/foo/' => 'foo',
'/' => 'username'
);
foreach($rules as $pattern => $action) {
if (strpos($url, $pattern) === 0) {
// use action
$file = "app/$action.php";
require $file;
exit;
}
}
// error handling - 404 no route found
这篇关于htaccess以及PHP - 重定向和pretty的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!