1

I need to use the urlencode() function which will make urls contain '%' sign.

My current rewrite does not allow these characters so i need to update it and what ive tried crashes the whole server.

Sample URL/A tag: <a href="/edit-portfolio/MQ%3D%3D">Test Link</a>

Old .htaccess (Working great untill i needed urlencode())

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9a-zA-Z_-]+)$ index.php?page=$1&c=$2 [NC,L]

New .htaccess (Crashes site)

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-%]+)/([0-9a-zA-Z_-%]+)$ index.php?page=$1&c=$ [NC,L]
ii iml0sto1
  • 1,654
  • 19
  • 37
  • What do you mean by "crashes site"? Do you get a 404 error or something you didn't expect? – Amit Verma Feb 18 '18 at 17:31
  • This is what i get when i use the new access file: Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at [no address given] to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. – ii iml0sto1 Feb 18 '18 at 17:37
  • You can't match against encoded `=` in pattern of a RewriteRule. To match against `%3D` you need to include a `=` to your regex character class ie : `[..........=]` – Amit Verma Feb 18 '18 at 18:08
  • @wiktorstribiżew The question is about how to match url decoded characters in RewriteRule while the linked question is about how to match hypens with regex and it's totally different. – Amit Verma Feb 19 '18 at 13:43
  • 1
    @wiktorstribiżew I understand that but RewriteEngine is a bit different . `-` isn't the actual reason of server crash .OP ' s existing regex pattern is almost good. But the question you put as duplicate doesn't make sense. – Amit Verma Feb 19 '18 at 14:24
  • @wiktorstribiżew I have already mentioned the reason in a previous comment to the question. The accepted answer worked because it uses a catch - all pattern instead of hard coded string or character - class and correctly matches all url - decoded characters. – Amit Verma Feb 19 '18 at 14:33

1 Answers1

2

You should take care of - in a character class because it may define a range. Just to be sure escape it all the time:

RewriteRule ^([0-9a-zA-Z_\-%]+)/([0-9a-zA-Z_\-%]+)$ index.php?page=$1&c=$ [NC,L]
                         ^                  ^

[_-%] this range isn't valid at all

or move it to the end of character class:

RewriteRule ^([0-9a-zA-Z_%-]+)/([0-9a-zA-Z_%-]+)$ index.php?page=$1&c=$ [NC,L]

By the way all could be shortened to:

RewriteRule ^([^/]+)/([^/]+)$ index.php?page=$1&c=$2 [NC,L]
                                                   ^ You missed this one
revo
  • 47,783
  • 14
  • 74
  • 117
  • Thats pretty neat i were not at all aware of this. I accepted your answer, thanks alot im relly impressed with the last short one i still cant figure out how that works but i will dive into it :) – ii iml0sto1 Feb 18 '18 at 18:29