如何使用 php 中的 htaccess 更改请求的 GET 数据来更改已经工作的 GET 查询 [英] How to change already working GET query with the change of requested GET data using htaccess in php
问题描述
我确定通过在 stackoverflow 中发布需求,我可以获得解决方案.
I'm sure by posting here in stackoverflow with the requirements I can get the solutions.
我正在开发一个 Web 应用程序,其中有页面,并且每个页面的内部都有帖子功能和问题.我在 SQL posts
和 questions
中有两个表.现在我想要的是:
1:当用户点击帖子链接(页面内)时,用户必须重定向到一个带有 SEO 友好 URL 的帖子页面,该页面正在工作.
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
用于 GET
页面查询 titles
用于 GET
发布查询 question
现在我需要在我用于发布 GET
查询的同一页面上进行 GET
问题查询.
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:当用户点击问题链接(页面内)时,用户必须重定向到单个问题页面.
2: When a user clicks on the question link (inside in page), the user has to redirect to a single question page.
工作 example.com/pages/my-page/my-page-first-Post
必需 example.com/pages/my-page/my-page-first-Question
此数据来自两个不同的 SQL 表
推荐答案
以下是使用 3 个单独规则的方法:
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]
在您的 page.php
代码中,您可以检查:
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')
}
这篇关于如何使用 php 中的 htaccess 更改请求的 GET 数据来更改已经工作的 GET 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!