我想重写我的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
对于这些,我很乐意保留“picture”并删除.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]
但不知道从哪里开始,相反的……将漂亮的URL转换回标准。如果能向我解释下一节,会有帮助吗?
^((?!web/)[^/.]+)/?$ /index.php?$1 [L,QSA,NC]