【发布时间】:2019-04-25 22:46:33
【问题描述】:
我正在尝试重写我的 URL 以删除 index.php?但我正在努力让它发挥作用。我能得到的最接近的是这里的答案:remove question mark from 301 redirect using htaccess when the user enters the old URL
我需要在输出时将 URL 转换为漂亮的 URL,并在输入时将它们重写回正确的 URL。URL 的结构如下:
https://sub.domain.com/index.php?/folder1/folder2-etc
使用引用答案中的代码会导致双斜杠:
https://sub.domain.com//folder1/folder2-etc
我从参考答案中使用的重写规则是:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php$ /$1 [L,R=301,NC,NE]
RewriteCond %{THE_REQUEST} \s/+\?([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteRule ^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]
我怀疑我知道如何解决第一个问题,但我很难理解内部转发的第二个规则。
此外,我想知道这是否是最好的方法。我目前正在 Nginx 反向代理后面运行 Apache 后端。我会更好地在 Nginx 端进行重写并在 Apache 上进行内部转发吗?
编辑:
复杂性:我注意到一个使事情复杂化的额外结构。一些 URL 似乎有https://sub.domain.com/picture.php?/folder1/folder2-etc
对于这些,我很乐意保留“图片”并删除 .php?位。
我猜首先,我需要做以下事情:
RewriteCond %{THE_REQUEST} \s/+index\.php\?/([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/+picture\.php\?/([^\s&]+) [NC]
RewriteRule ^(.*)$ /picture/%1 [R=301,L]
但不知道从哪里开始相反...。即将漂亮的网址转换回标准。如果可以向我解释以下部分会有所帮助吗?
^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]
【问题讨论】:
标签: regex apache .htaccess mod-rewrite url-rewriting