3

I have the following .htaccess code:

RewriteRule ^Indian-Patriot-Name/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^\x00-\x7F]+).*?$ indianpatriot.php?id=$1&lid=$2&name=$3  [B,L,QSA]

Which allows me to view the result of the following link:

example.com/Indian-Patriot-Name/104/8/ச்வடன்ற்யவீர்%20சவர்க்கார்%20రాష్ట్రీయ%20சமரக்

However, when I want to view the following URL (not unicode):

example.com/Indian-Patriot-Name/104/8/abcd

I am unable to view the output as I'm getting a "file not found" error! However, if I change the directive/regex to the following then I can read the above (non-unicode) URL properly.

RewriteRule ^Indian-Patriot-Name-English/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ indianpatriot.php?id=$1&lid=$2&name=$3  [L]

Q. How can I combine ([A-Za-z0-9-]+) and ([^\x00-\x7F]+) in the same rewrite statement?

I tried:

RewriteRule ^Indian-Patriot-Name-English/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^\x00-\x7F]+)+([A-Za-z0-9-]+)?$ indianpatriot.php?id=$1&lid=$2&name=$3  [B,L,QSA]

but I'm getting a "file not found" error again.


UPDATE: I tried the following:

RewriteRule ^Indian-Patriot-Name-English/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^\x00-\x7F]+ [A-Za-z0-9-]+)?$ indianpatriot.php?id=$1&lid=$2&name=$3  [B,L,QSA]

But I get an Internal Server Error:

Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator at [email protected] 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.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

7
  • As with all internal errors, check your logfiles, Apache will have tell you there what is wrong (besides the fact that it had searched for something to display as a 500 ErrorDocument and it was not found due to invalid configuration) Commented Dec 20, 2017 at 15:37
  • Also I do not understand your problem. You can not "combine" ([A-Za-z0-9-]+) and ([^\x00-\x7F]+) as something matching the first specification can not match the second one (the characters in the first are indeed in the \x00-\x7F range that the second one want to exclude) Commented Dec 20, 2017 at 15:39
  • let me redefine this rewrite ([^\x00-\x7F]+) works fine. How can I allow A-Za-z0-9- too in ([^\x00-\x7F]+) Commented Dec 20, 2017 at 17:26
  • 1
    Still not clear to me, but try ([^\x00-\x7F]+|[A-Za-z0-9-]+) Commented Dec 20, 2017 at 17:28
  • let me redefine this. " rewrite ([^\x00-\x7F]+) " works fine for unicode only. I wan to allow A-Za-z0-9- character set in this rewrite statement i.e.Unicode and A-Za-z0-9 together in same rewrite statement Commented Dec 20, 2017 at 17:40

1 Answer 1

1
RewriteRule ^Indian-Patriot-Name-English/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^\x00-\x7F]+ [A-Za-z0-9-]+)?$ indianpatriot.php?id=$1&lid=$2&name=$3 [B,L,QSA]

I get Internal Server Error ...

You have an erroneous space in the RewriteRule pattern. This will result in your "Internal Server Error" since spaces are delimiters in Apache directives. If you check your error log you'll probably see an error about "Invalid flags", since indianpatriot.php?id... will now be seen as the flags argument to the RewriteRule directive.

However, the suggestion by @PatrickMevzek in comments, to use alternation looks as if it should have worked. For example:

RewriteRule ^Indian-Patriot-Name-English/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([^\x00-\x7F]+|[A-Za-z0-9-]+)?$ indianpatriot.php?id=$1&lid=$2&name=$3 [B,L,QSA]

Note that your original regex that matches the unicode URL had an additional "catch everything else" pattern on the end: .*?. (Your regex that matches the non-unicode URL does not have this.) This does seem unusual. If this is required then you can simply omit the $ (end of string anchor) from the end of the pattern to do the same thing.

Note also, that [^\x00-\x7F] is a negated character class, that matches any character not in the byte range 00..7F (hex). In order to incorporate the latin alphabet, that falls in this range, then you could simply extend this range: [\x00-\xFF] - this is now a positive range that matches everything (which is no doubt more than you need, but you were probably matching more than you needed with the original regex).

However, this could perhaps be simplified. If you simply want to match anything (unicode or latin characters) at the end of the URL then ([^\x00-\x7F]+|[A-Za-z0-9-]+) (or even ([\x00-\xFF]+)) could be simplified to ([^/]+) (ie. anything except a slash). And, providing you don't also mind matching the underscore character, then [A-Za-z0-9-] could be reduced to [\w-]. So, this becomes:

RewriteRule ^Indian-Patriot-Name-English/(\w-]+)/([\w-]+)/([^/]+)?$ indianpatriot.php?id=$1&lid=$2&name=$3 [B,L,QSA]

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.