Forum Moderators: phranque
RewriteRule ^([a-z0-9_-]+)/?([a-z0-9_-]+)?(\.png¦/)?$ generation/image.php?user=$1&template=$2 [NC,L] It accepts URLs that look like the following:
user_string_here/template_string_here(.png)
And obviously, the .png is optional.
Now the problem is I want to pass URL encoded characters to this. When I change it to look like this:
RewriteRule ^([a-z0-9_-%]+)/?([a-z0-9_-]+)?(\.png¦/)?$ generation/image.php?user=$1&template=$2 [NC,L] I get a server error. Any idea on how I can allow url encoded characters to pass through here?
Be aware that hyphen/dash is a special character within alternate groups, specifying a "character range" as in "a-z" in your pattern. Unfortunately, handling is buggy, and so it's a good idea to always escape that character, as in "[a-z0-9_\-]". If indeed you need to include "%" in your group, then escaping the hyphen or putting the "%" before the hyphen will likely fix your server error.
I also suggest that you include the first slash within the optional group to speed up parsing and make the assignment of URL-path-parts to back-references more predictable:
RewriteRule ^([a-z0-9_\-%]+/)?([a-z0-9_\-]+)(\.pngĻ/)?$ generation/image.php?user=$1&template=$2 [NC,L]
For more info, search for "duplicate content" and "canonical URL" in our Google forum.
Jim
The slash is outside of the first group because I'm parsing the values with PHP, and if I leave the slash in I have to parse it out inside the PHP code anyway.
In the original code, if I visit:
generation/image.php?user=test-%23test&template=test
It returns test-#test and test as the values, respectively. However, if I visit:
/test-%23test/test
I get a 404.
If you need to leave it encoded, then try the following:
Use a RewriteCond examining %{THE_REQUEST} to get the original encoded client request into a variable (e.g. $1). Then back-reference that variable in the rewriterule substitution, and use the [NE] flag (if necessary) to prevent double-encoding.
To exclude the trailing slash on the path, use "^(([a-z0-9_\-%]+)/)?([a-z0-9_\-]+)(\.pngĻ/)?$" and then use $2 and $3 instead of $1 and $2 in the substitution. This puts the "optional-path-boundary" where it should be, but excludes the trailing slash.
Jim
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.]+\ HTTP/
RewriteRule ^(([a-z0-9_\-%]+)/)?([a-z0-9_\-]+)(\.pngĻ/)?$ generation/image.php?user=$2&template=$3 [NC,L] Now, I don't understand how to use the backreference from RewriteCond in RewriteRule.
I did some research on the issue I'm having, and the "easy" fix is usually recommended as being double-encoding the URL. However, that won't work in this case since I'm not generating the URLs.
[httpd.apache.org...]
Adding the B flag to my RewriteRule has apparently solved the problem.