[英]How to redirect multiple to a single page but with different URLs
I am a beginner in practical web development.My situation is as follows 我是实际Web开发的初学者。我的情况如下
I have a number of questions in my database and I provide a long list of links clicking on which the user can see each question in more detail.The strategy I am using is something like 我的数据库中有很多问题,我提供了一长串链接,单击链接后用户可以更详细地看到每个问题。我使用的策略类似于
www.mydomain.dom/questions/view.php?id=465
ie I am passing the id
of the question as GET to view.php
which displays details. 即我将问题的
id
作为GET传递给显示详细信息的view.php
。
Now I want the URL something like 现在我想要类似的URL
www.mydomain.com/questions/category_of_question/title_of_question/
ie I want to append the question details from the database as if it appears that I have different page for each question though at the backend I am still using the page view.php
. www.mydomain.com/questions/category_of_question/title_of_question/
即我想从数据库中附加问题详细信息,好像每个问题都有不同的页面,尽管在后端我仍在使用view.php
页面。
I have heard of pretty URLs where you can hide page extension but I couldn't how I can use it in my scenario. 我听说过漂亮的URL,您可以在其中隐藏页面扩展名,但是我无法在我的场景中使用它。
Any help? 有什么帮助吗?
If you are using Apache, Enable mod_rewrite. 如果使用的是Apache,请启用mod_rewrite。
And add a .htaccess 并添加一个.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
Now you can use the url like this, www.mysite.com/1/, it's the same as this www.mysite.com/view.php?id=1 . 现在,您可以使用这样的URL,即www.mysite.com/1/,与此www.mysite.com/view.php?id=1相同。
This will transfer all the urls in your website to the view.php 这会将您网站中的所有网址传输到view.php
If you want it only with a specific keyword (question) do this, you need to change the last line with this one. 如果只希望使用特定的关键字(问题)来执行此操作,则需要用此关键字更改最后一行。
RewriteRule ^question/(.*)$ view.php?id=$1
Now your url would look like, mywebsite.com/question/1 现在您的网址应为mywebsite.com/question/1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.