1

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.

2 Answers 2

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]
Sign up to request clarification or add additional context in comments.

6 Comments

Hmm. This doesn't seem to change the url at all. Not sure why.
What URL are you entering in the browser? Do you want to see example.com/prefix-suffix.html?v=huzzah in the browser or want to see example.com/prefix/suffix/huzzah in the browser?
End result is that I would want to see example.com/prefix/suffix/huzzah in the browser.
Ok I will edit. Meanwhile if you enter example.com/prefix/suffix/huzzah in browser does this rule display correct content from example.com/prefix-suffix.html?v=huzzah?
Thanks. Works great. I wish I understood it more. :)
|
1

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.

1 Comment

Thanks a lot for taking the time to explain all of that. I appreciate it greatly. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.