如果不存在,重写后添加正斜杠

adding forward slash after rewrite if not exist

我阅读了大量内容并尝试了很多解决方案,但找不到适合我需要的解决方案。

在我的 htaccess 中我有以下内容:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([\w-]+)/?$ profile.php?username= [L,QSA]
RewriteRule ^profile/([\w-]+)/([\w-]+)/?$ profile.php?username=&type= [L,QSA]
RewriteRule ^profile/([\w-]+)/([\w-]+)/([\w-]+)/?$ profile.php?username=&type=&o= [L,QSA]

除 1 个小问题外,效果非常好。

如果 profile/username 没有 /,除非使用绝对 URL,否则页面上的链接将中断。所以 <a href="./1"> 最终会变成 profile/1 而不是 profile/username/1

如果我将第一个重写更改为以下内容:

RewriteRule ^profile/([\w-]+)/$ profile.php?username= [L,QSA]

然后 https://myurl/username 将 return 一个我不想要的 404 - 如果它不存在,我希望它在末尾强制 /。

我试过添加:

RewriteRule ^(.*)([^/])$ // [L,R=301] 

我试过了

RewriteRule ^(.*)$ https://nmyurl// [L,R=301]

只是不知道如何在重写条件已经到位的情况下做到这一点。

要在您的 profile 网址末尾添加可选的跟踪,您可以使用此

RewriteEngine on

RewriteCond %{REQUEST_URI} ^/profile/
RewriteRule !/$ %{REQUEST_URI}/ [L,R]

If profile/username does not have a / the links on the page will break unless absolute urls are used. So <a href="./1"> will end up as profile/1 instead of profile/username/1

如果问题仅适用于 /profile/<username> 形式的 URL,那么我会具体化,只将尾部斜杠附加到这些特定的 URL,否则,您是将获得大量重定向(这可能对 SEO 不利)。

但是,您应该确保内部链接是针对规范的 URL(即带有尾部斜杠)。

例如,以下内容应该在 之前 您现有的重写:

RewriteRule ^(profile/[\w-]+)$ // [R=301,L]

由于规范 URL 需要尾部斜线,因此这应该是 301(永久)重定向。 (但先用 302 测试。)


或者,不是在 .htaccess 中重定向(如果您仍在内部链接到无斜杠 URL),那么您可以添加一个 base 元素(包括尾部斜杠)到 head 部分说明相对 URL 应该相对于什么。

例如:

<base href="/profile/<username>/">

旁白:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/([\w-]+)/?$ profile.php?username= [L,QSA]
RewriteRule ^profile/([\w-]+)/([\w-]+)/?$ profile.php?username=&type= [L,QSA]
RewriteRule ^profile/([\w-]+)/([\w-]+)/([\w-]+)/?$ profile.php?username=&type=&o= [L,QSA]

这两个 RewriteCond 指令似乎完全是多余的。 RewriteCond 指令仅适用于随后的第一个 RewriteRule。但是 RewriteRule 模式 不太可能匹配真实文件(除非你有没有扩展名的文件或同名目录)。

所以不幸的是我无法真正实现我希望的,因为有多个级别的变量可能存在也可能不存在。

部分我使用了Amit提供的解决方案:

RewriteCond %{REQUEST_URI} ^/profile/
RewriteRule !/$ %{REQUEST_URI}/ [L,R]

但这还不够,因为正如 MrWhite 所指出的那样,有 3 个独立的潜在 url。

https://myurl/profile/username/
https://myurl/profile/username/type/
https://myurl/profile/username/type/o/

在此站点中,用户名应始终存在,但类型和 o 可能存在也可能不存在。

所以我所做的是检测 url 的级别,然后创建条件 .和 .. 使用 php.

变量 o 始终是数字,变量类型从不是数字,所以这对我有用。

if (isset($_GET['o'])) { $o = strip_tags($_GET['o']); }
elseif (isset($_GET['type']) && is_numeric($_GET['type'])) { $o = strip_tags($_GET['type']); }

然后我检测到:

// if o is set or if type is numberic, use ..
if (isset($_GET['o']) || (isset($_GET['type']) && is_numeric($_GET['type']))) {
  $dots = '..';
// if o is not set and type is not numeric just use .
} else {
  $dots = '.';
}

<a href="'.$dots.'/1/">1</a> 的最终结果:

if url is https://myurl/profile/username/
result is https://myurl/profile/username/1/

if url is https://myurl/profile/username/3/
result is https://myurl/profile/username/1/

if url is https://myurl/profile/username/type/3/
result is https://myurl/profile/username/type/1/

这是期望的结果。