【问题标题】:How to Rewrite a URL with an Id and text using htaccess如何使用 htaccess 重写带有 ID 和文本的 URL
【发布时间】:2013-05-30 03:50:02
【问题描述】:

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

【问题讨论】:

  • 请澄清:我假设您希望浏览器栏中的网址是这个:http://www.example.com/114/this-should-not-happen-again
  • @goodeye 你是对的。

标签: regex .htaccess url-rewriting


【解决方案1】:

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

RewriteEngine On

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

【讨论】:

    【解决方案2】:

    通过httpd.conf启用mod_rewrite和.htaccess,然后把这段代码放到你.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]
    

    【讨论】:

    • 我认为这颠倒了 OP 想要什么。您正在将他想要的结果改写为他最初的请求。
    【解决方案3】:
    RewriteEngine on
    RewriteRule ^/post.php?post_id=(\d+)&post_txt=(.*)$ http://www.example.com/$1/$2 [R=301,L]
    

    另外,超级用户here也存在一个非常相似的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-06
      • 1970-01-01
      • 2020-04-09
      • 2011-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多