【问题标题】:.htaccess from dynamic page url to static with variables.htaccess 从动态页面 url 到带有变量的静态
【发布时间】:2017-10-08 13:40:50
【问题描述】:
我想创建一个动态规则(可能是 .htaccess)来重定向页面:
https://www.example.com/dir/search?q=stack+overflow
到
https://www.example.com/dir/stack-overflow
我知道我需要一个 /dir 文件夹
我试过了
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)\. search?q=$1
不工作。
【问题讨论】:
标签:
php
.htaccess
mod-rewrite
dynamic-url
【解决方案1】:
我假设您的意思是从 /dir/search?q=stack-overflow 到 /dir/stack-overflow 的 301 重定向,也就是说,如果用户输入 https://www.example.com/dir/search?q=stack-overflow,浏览器 URL 将更改为 https://www.example.com/dir/stack-overflow。问题是,/dir/stack-overflow 是真正的文件夹还是文件?我不这么认为,因此您必须将/dir/stack-overflow 重写回/dir/search?q=stack-overflow。
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^q=([^&]*)
RewriteRule ^/?dir/search/?$ /dir/%1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/?dir/search/?$ [NC]
RewriteRule ^/?dir/([^/?]+)/?$ /dir/search?q=$1 [L]
除了删除最后两行之外,我不想重写。