htaccess URL Rewrite with and without query string

那年仲夏 提交于 2019-12-24 06:43:14

问题


I'm trying to rewrite some urls but my newbness is getting in the way.

URLs input into the address bar

example.com/prefix-suffix.html
example.com/prefix-suffix.html?v=huzzah

Desired output (would like them displayed like)

example.com/prefix/suffix/
example.com/prefix/suffix/huzzah

I've looked at a few other examples like this one, but I just don't understand what they are doing exactly or how rather, which makes it challenging to modify.

If you have the time to explain what each line is doing I would greatly appreciate it.

Thanks.


回答1:


You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+([^-]+)-([^.]+)\.html\s [NC]
RewriteRule ^ /%1/%2/ [R=302,L,NE]

RewriteCond %{THE_REQUEST} \s/+([^-]+)-([^.]+)\.html\?v=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L,NE]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^([^/]+)/([^/]+)/?$ /$1-$2.html [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1-$2.html?v=$3 [L,QSA]



回答2:


this is a good reference. to summarize the relevant parts:

in the SO question you are referring to, the OP wanted index.php?page=mobile to become index/page/mobile

and the accepted answer to that question was:

RewriteEngine on
RewriteBase /cashearn/

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/page/%1? [R=302,L]

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



in order to understand how that is working, let's take a close look at this section:

RewriteCond %{THE_REQUEST} /index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ index/page/%1? [R=302,L]

RewriteCond is a condition that must be matched in order for the subsequent RewriteRule to be used. the pattern-matching is done using regex. the basics are easy to master so i recommend that you look it up. in any case, anything of the form %N (e.g. %1) is a backreference to the "grouped" part of the pattern of the last RewriteCond in the current set of conditions. A "grouped" part is denoted by parentheses. In other words, the %1 in the RewriteRule refers to the ([^\s&]+) in the RewriteCond.

looking at the regex:
square brackets in regex denote a character class so [^\s&] is thus a character class. the caret ^ when it is inside a character class denotes negation. \s is an escape code for the whitespace character. so, all in all, [^\s&] means "any character except whitespace and &". the character class is appended with + which means "one or more". so, the regex pattern will match one of characters that are included in the character class. for a url, this essentially means any combination of letters, digits and %.

the other characters in the RewriteRule and RewriteCond, other than the "server-variable" %{THE_REQUEST}, are either regex special characters or literals. (by literals i mean that ab in a regex will match the string cab.)

^, the caret, when it isn't inside a character class, is a special character that denotes the beginning of a line. note that ? and . are also literals here, despite the fact that they are included in the list of regex "special characters". that is because they are escaped with a \.

the only thing left to explain is the flags [NC] and [R=302,L]
NC means case-insensitive. L means last, i.e. if there is a match, then subsequent rules will not be processed. 'R' means "redirect" and 302 is the redirect's HTTP status code.

the difference between an internal and external redirect is relevant here. with an internal redirect, the server will silently grab the resources from the filepath specified at the newly formed URL, while the user still sees the original URL in the browser. the R flag indicates an external redirect and this causes the user to initiate a new HTTP transaction with the newly formed URL. Omit R for an internal redirect.



来源:https://stackoverflow.com/questions/26784082/htaccess-url-rewrite-with-and-without-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!