URL重写 - 查询字符串重写、字符串、URL

2023-09-02 11:25:57 作者:比较有个性的昵称

我有一个新闻(博客)的网站时,个别职位的选择,它返回的URL格式如下:

I have a news (blog) site that returns urls in the following format when individual posts are selected:

website.net/sitenews.php?q=posts/view/postname/12

我试图重写URL,这样记载:

I am seeking to rewrite the url so that it reads:

website.net/sitenews.php/posts/view/postname/12

或其他任何方式,其中?Q = 去除的目的是去除这样的网址可以通过Facebook的喜欢按钮,Facebook的网址短绒不解析查询字符串进行访问。

or any other way where the ?q= is removed for purpose of removing the ? so that the url can be accessed by facebook's like button as the facebook url linter does not parse query strings.

在htdocs目录的.htaccess中我曾尝试下面的根目录下的文件:

In the htdocs .htaccess file in the root directory I have tried the following:

Options +FollowSymLinks 

RewriteEngine on

RewriteCond %{QUERY_STRING} q=  

RewriteRule (.*) website.net/sitenews.php/$1? [R=301]

这成功地消除了字符串 Q =?但是其余的(职位/浏览/ postname / 12 )不回来了,URL现在看起来如下:

This successfully removes the q=? however the rest of the string (posts/view/postname/12) is not returned and the url now looks as follows:

 website.net/sitenews.php/sitenews.php

没有人有任何建议,以帮助我完成这个url_rewrite?

Does anyone have any suggestions to help me complete this url_rewrite?

推荐答案

试试这个,而不是在你的.htaccess:

Try this instead in your .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$ [NC]
RewriteRule ^(.*)$ /$1/%1? [R=301,L,NE]

R=301 will redirect with https status 301
L will make last rule
NE is for no escaping query string

%1 is capture group for query string q= (whatever comes after q=)
$1 is your REQUEST_URI