InsaneCoder
InsaneCoder

Reputation: 8268

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

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

i.e I am passing the id of the question as GET to view.php which displays details.

Now I want the URL something like

www.mydomain.com/questions/category_of_question/title_of_question/ i.e 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.

I have heard of pretty URLs where you can hide page extension but I couldn't how I can use it in my scenario.

Any help?

Upvotes: 1

Views: 345

Answers (1)

Jorge Faianca
Jorge Faianca

Reputation: 791

If you are using Apache, Enable mod_rewrite.

And add a .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 .

This will transfer all the urls in your website to the 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

Upvotes: 1

Related Questions