简体   繁体   中英

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.

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]

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

My htaccess handles the rewrite and redirect of the url correctly, but only when the {QUERY_STRING} has just one argument that is passed.

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.

How should I go about writing my RewriteCond / RewriteRule to handle urls with either 1 or or more {QUERY_STRING} arguments?

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. 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]

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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