简体   繁体   English

htaccess将具有多个查询字符串args的url重定向到SEO友好的url

[英]htaccess redirect url with multiple query string args to SEO friendly url

I have crawled StackExchange for 3 days now and come close to finding the solution to my problem, but keep coming up short. 我已经对StackExchange进行了3天的爬网,并且接近找到解决我的问题的方法,但是一直很简短。

I am using htaccess to rewrite and redirect urls to SEO friendly urls. 我正在使用htaccess重写URL并将其重定向到SEO友好URL。 My htaccess currently is as follows. 我的htaccess当前如下。

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]

Currently my links pass 1 main argument '?page=somepage'. 目前,我的链接传递了1个主要参数'?page = somepage'。 On my users page, I have links that pass not only the page argument, but also the id of a user. 在我的用户页面上,我的链接不仅传递page参数,而且传递用户的ID。 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

My htaccess handles the rewrite and redirect of the url correctly, but only when the {QUERY_STRING} has just one argument that is passed. 我的htaccess可以正确处理url的重写和重定向,但是仅当{QUERY_STRING}仅传递了一个参数时。

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. 我已经在使用现有的RewriteCond来查找查询字符串中的多个参数,并且还尝试更改RewriteRule以处理查询字符串中的多个参数。 I was successful when dealing with 2 arguments, but my original rule (for 1 argument) broke. 我在处理2个参数时很成功,但是我原来的规则(1个参数)坏了。

How should I go about writing my RewriteCond / RewriteRule to handle urls with either 1 or or more {QUERY_STRING} arguments? 我应该如何编写RewriteCond / RewriteRule来处理带有1个或多个{QUERY_STRING}参数的网址?

I want my urls to go from : 我希望我的网址来自:

?page=users
?page=users&id=1
?page=users&id=1&do=edit

to this : 对此:

/users
/users/1
/users/1/edit

After tinkering with my htaccess file, I managed to find a solution to my problem. 修改了htaccess文件后,我设法找到了解决问题的方法。 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. 以下是我更新的,可以正常工作的htaccess文件。

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]

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? 当这满足我的需要时,我确实还有另一个问题。...是否可以简化此文件中的htaccess RewriteCond / RewriteRule?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM