htaccess 将带有多个查询字符串参数的 url 重定向到 SEO 友好的 url
我已经爬了 3 天的 StackExchange 并接近找到我的问题的解决方案,但一直保持简短.
I have crawled StackExchange for 3 days now and come close to finding the solution to my problem, but keep coming up short.
我正在使用 htaccess 重写网址并将其重定向到 SEO 友好的网址.我的htaccess目前如下.
I am using htaccess to rewrite and redirect urls to SEO friendly urls. My htaccess currently is as follows.
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]
##################################################
# REWRITE QUERY STRING INTO SEO FRIENDLY URL
##################################################
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?page=([^\s]+)? [NC]
RewriteRule ^ /%1? [R=301,L]
目前我的链接传递 1 个主要参数?page=somepage".在我的用户页面上,我的链接不仅传递页面参数,还传递用户的 id.该链接如下:
Currently my links pass 1 main argument '?page=somepage'. On my users page, I have links that pass not only the page argument, but also the id of a user. That link is as follows :
?page=users&id=1
此外,用户可以选择编辑自己的个人资料,因此该链接如下所示:
Furthermore, a user has the option to edit their own profile, so that link is like this :
?page=users&id=1&do=edit
我的 htaccess 正确处理了 url 的重写和重定向,但仅当 {QUERY_STRING} 只传递了一个参数时.
My htaccess handles the rewrite and redirect of the url correctly, but only when the {QUERY_STRING} has just one argument that is passed.
我使用现有的 RewriteCond 在查询字符串中查找多个参数,并尝试更改 RewriteRule 以处理查询字符串中的多个参数.我在处理 2 个参数时成功了,但是我的原始规则(对于 1 个参数)破坏了.
I have played with my existing RewriteCond to look for multiple arguments in the query string and also tried changing the RewriteRule to handle multiple arguments in the query string. I was successful when dealing with 2 arguments, but my original rule (for 1 argument) broke.
我应该如何编写我的 RewriteCond/RewriteRule 来处理带有 1 个或多个 {QUERY_STRING} 参数的网址?
How should I go about writing my RewriteCond / RewriteRule to handle urls with either 1 or or more {QUERY_STRING} arguments?
我希望我的网址来自:
?page=users
?page=users&id=1
?page=users&id=1&do=edit
到这个:
/users
/users/1
/users/1/edit
在修改我的 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?