php - 将用户从动态网址重定向到静态网址(带有两个参数)

标签 php apache .htaccess url mod-rewrite

我的网站上有一个 URL,其结构为:
- mywebsite.com/directory/object.php?id=[id]&title=[标题]
使用 .htaccess 文件,我已将上述动态 URL 更改为以下内容:
- mywebsite.com/directory/[id]/[title] 类似于此问题的 stackoverflow 的 URL..

我想知道如何将输入旧动态 URL 的任何用户重定向到新静态 URL。我知道你使用 RewriteCond 但我无法弄清楚代码。

将动态 URL 更改为静态 URL 的我的 .htaccess 文件代码包含:

    RewriteEngine On
    RewriteRule ^directory/([^/]*)/([^/]*)$ /directory/object.php?id=$1&title=$2 [L]

提前致谢!

最佳答案

这样吧:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+(directory)/object\.php\?id=([^&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

RewriteRule ^directory/([^/]+)/([^/]+)/?$ /directory/object.php?id=$1&title=$2 [L,QSA]

关于php - 将用户从动态网址重定向到静态网址(带有两个参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21365536/

相关文章:

php - 如何通过 Cpanel 每天 6 点进行 cron 作业

php - MySQL 与 PHP 书

wordpress - .htaccess 用两个变量重写 url

python - 使用 Python 请求信任自签名证书

php - 使用重写规则从 url 中删除 .php 后,css 和 img 文件有 404

php - 如何隐藏 laravel 中的关系列?

javascript - 单击一行时重定向到操作的正确方法(onclick)Yii2

regex - htaccess : redirect old domain and all pages to a new domain

PHP:创建文件但不能附加(显然)正确的服务器权限(LAMP)

apache - 对某些页面强制使用 https,对所有其他页面强制使用 http