且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

htaccess 将带有多个查询字符串参数的 url 重定向到 SEO 友好的 url

更新时间:2023-10-21 14:28:04

在修改我的 htaccess 文件后,我设法找到了解决我的问题的方法.这只是证明,您正在寻找的答案往往是最明显和最容易被忽视的.以下是我更新的、有效的 htaccess 文件.

After tinkering with my htaccess file, I managed to find a solution to my problem. This just proves that often the answer you are searching for is the most obvious and most overlooked. The following is my updated, and working htaccess file.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

########################################
#   REMOVE INDEX.PHP FROM THE URL
########################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]

##################################################
#   RERWITE QUERY STRING INTO SEO FRIENDLY URL
##################################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)? [NC]
RewriteRule ^ /%1? [R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)&id=([^\s]+)? [NC]
RewriteRule ^ /%1/%2? [R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)&id=([^\s]+)&do=([^\s]+)? [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

虽然这可以满足我的需求,但我还有另一个问题......有没有办法简化这个文件中的 htaccess RewriteCond/RewriteRule?

While this is working for my needs, I do have another question.... Is there a way to simplify my htaccess RewriteCond / RewriteRule in this file?