Regex 如何使用htaccess重写带有Id和文本的URL

Regex 如何使用htaccess重写带有Id和文本的URL,regex,.htaccess,url-rewriting,Regex,.htaccess,Url Rewriting,实际上,我不知道regex等,我想用.htacess重写URL。我有以下网址 http://www.example.com/post.php?post_id=114&post_txt=this-should-not-happen-again 我想这样重写它 http://www.example.com/114/this-should-not-happen-again 我应该在.htaccess文件中写入什么来实现我的目标?通过httpd.conf启用mod_rewrite和.htac

实际上,我不知道regex等,我想用.htacess重写URL。我有以下网址

http://www.example.com/post.php?post_id=114&post_txt=this-should-not-happen-again
我想这样重写它

http://www.example.com/114/this-should-not-happen-again

我应该在.htaccess文件中写入什么来实现我的目标?

通过
httpd.conf
启用mod_rewrite和.htaccess,然后将此代码放在
文档根目录下的
.htaccess
中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /post.php?post_id=$1&post_txt=$2 [L,QSA]
此外,超级用户上也存在一个非常类似的问题,请尝试一下: 它查找数字、斜杠或任何字符。它在
$1
中插入数字,并在
$2
中插入任意字符

RewriteEngine On

RewriteRule ^(\d+)/(.+)$ /post.php?post_id=$1&post_txt=$2 [NC,L]

我认为这正是OP想要的。您正在将他期望的结果改写为他的原始请求。请澄清:我假设您希望浏览器栏中的url为以下url:
http://www.example.com/114/this-should-not-happen-again
?@goodeye你说得对。
RewriteEngine On

RewriteRule ^(\d+)/(.+)$ /post.php?post_id=$1&post_txt=$2 [NC,L]