如果 url 只有 1 个段,则创建 mod 重写


Create mod rewrite if the url has only 1 segment

我想请一些关于 Mod 重写的帮助。

我想仅在网址如下所示时才重写 mod:

https://example.com/segment
https://example.com/segment/

我想将网址指向

https://www.example.com/?r=segment

然后重定向到

https://www.example.com/

非常感谢!您的帮助将不胜感激。

试试这个

RewriteEngine on
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$
RewriteCond %{QUERY_STRING} ^$
RewriteRule .* /?r=%1 [R,L]

完成测试重定向是否应该是永久性的后,将[R,L]替换为[R=301,L](主要出于 SEO 原因有用)。

对于问题的"然后重定向到"部分,在您的/index.php 脚本中,当您准备好重定向时,请输入以下内容:

header('Location: /');
exit;