How to change already working GET query with the change of requested GET data using htaccess in php

Viewed 58

I'm sure by posting here in stackoverflow with the requirements I can get the solutions.

I'm working on a web application where I have pages and inside on each page, there are posts features and questions. For which I have two tables in SQL posts and questions. Now What I want is:
1: When a user clicks on the post link (inside in page), the user has to redirect to a single post page with SEO friendly URLs which is working.

RewriteRule ^pages/([\w-]+)/?$ page.php?pages=$1 [QSA,NC,L]
RewriteRule ^pages/([\w-]+)/([\w-]+)/?$ page.php?pages=$1&title=$2 [QSA,NC,L]
pages is for GET page query
titles is for GET post query
question now i need for GET Question query on the same page which I used for post GET query.

2: When a user clicks on the question link (inside in page), the user has to redirect to a single question page.

Working example.com/pages/my-page/my-page-first-Post
Required example.com/pages/my-page/my-page-first-Question

This data comes from two different SQL tables

1 Answers

Here is how you can use 3 separate rules:

RewriteEngine On

# /pages/my-page
RewriteRule ^pages/([\w-]+)/?$ page.php?pages=$1 [QSA,NC,L]

# for URI /pages/my-page/my-page-first-Post
RewriteRule ^pages/([\w-]+)/([\w-]+)/?$ page.php?pages=$1&title=$2 [QSA,NC,L]

# for URI /pages/my-page/q/my-page-first-Question 
RewriteRule ^pages/([\w-]+)/q/([\w-]+)/?$ page.php?pages=$1&question=$2 [QSA,NC,L]

Inside your page.php code you can check:

if (isset($_GET['question')) {
    // question page
    // work with $_GET['page') and $_GET['question')
} else if (isset($_GET['title')) {
    // post page
    // work with $_GET['page') and $_GET['title')
} else {
    // single page
    // work with $_GET['page')
}
Related