如何将多个重定向到单个页面但具有不同的URL

时间:2014-01-14 10:18:21

标签: php .htaccess url web

我是实际网站开发的初学者。我的情况如下

我的数据库中有很多问题,我提供了很长的链接列表,用户可以更详细地查看每个问题。我使用的策略类似于

www.mydomain.dom/questions/view.php?id=465

即我将问题的id作为GET传递给显示详细信息的view.php

现在我希望网址类似

www.mydomain.com/questions/category_of_question/title_of_question/即我想从数据库中附加问题详细信息,就好像我的每个问题都有不同的页面,但在后端我还在使用页面view.php

我听说过可以隐藏页面扩展名的漂亮网址,但我无法在我的方案中使用它。

任何帮助?

1 个答案:

答案 0 :(得分:1)

如果您使用的是Apache,请启用mod_rewrite。

并添加.htaccess

# Turn on URL rewriting
RewriteEngine On

#Allow any files or directories that exist to be displayed directly
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

#Rewrite url to the variable id
RewriteRule ^(.*)$ view.php?id=$1

# Turn on URL rewriting RewriteEngine On #Allow any files or directories that exist to be displayed directly RewriteCond %{SCRIPT_FILENAME} !-f RewriteCond %{SCRIPT_FILENAME} !-d #Rewrite url to the variable id RewriteRule ^(.*)$ view.php?id=$1

现在您可以使用这样的网址www.mysite.com/1/,它与www.mysite.com/view.php?id=1相同。

这会将您网站中的所有网址转移到view.php

如果您只想使用特定关键字(问题)执行此操作,则需要使用此更改最后一行。

RewriteRule ^question/(.*)$ view.php?id=$1

现在你的网址看起来像mywebsite.com/question/1