0

实际上我不知道正则表达式等,我想使用 .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 文件中写什么来实现我的目标?

4

3 回答 3

1

试一试:它会查找数字、斜杠、任意字符。它在 中插入数字,在 中插入$1任意字符$2

RewriteEngine On

RewriteRule ^(\d+)/(.+)$ /post.php?post_id=$1&post_txt=$2 [NC,L]
于 2013-05-30T04:08:53.370 回答
0

通过启用 mod_rewrite 和 .htaccess httpd.conf,然后将此代码放在您.htaccessDOCUMENT_ROOT目录下:

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]
于 2013-05-30T04:05:41.450 回答
0
RewriteEngine on
RewriteRule ^/post.php?post_id=(\d+)&post_txt=(.*)$ http://www.example.com/$1/$2 [R=301,L]

此外,这里的超级用户也存在一个非常相似的问题

于 2013-05-30T04:06:21.523 回答